CSS 伪类与 jQuery

发布于 2024-07-13 00:25:30 字数 954 浏览 12 评论 0 原文

我刚刚学了一点 jQuery,并尝试用它来实现简单的变色效果。 假设我有两个 >,#foo 和 #bar。 #foo 有很多 URL,并且定义了以下 CSS:

#foo a {color: blue; border-bottom: 1px dashed blue}
#foo a:hover {color: black; border-bottom: 1px solid black}

现在我想在用户单击 #bar 时更改 #foo 中链接 (a:link) 的颜色,但保持 a:hover 颜色不变,所以我这样写我的函数:

//...
$("#bar").click(function () {
  $("#foo a").css("color", "red");
});
//...

问题是,虽然这个函数确实将所有链接更改为红色,但 a:hover 颜色丢失,即当用户将光标移到链接上时,它们将保持红色,而不是变成红色黑色如我所料。

因为我看到 jQuery 所做的就是将内联样式放入 #foo 内的 > 中,使它们变成 >,我想这会覆盖 :hover 伪类。 由于伪类的内联样式 attr 还没有被任何人实现,我怀疑我是否能得到我想要的效果......

仍然,有没有任何解决方案不需要我写类似的东西

$("#foo a").hover(
    function(){ $(this).css("color", "black");},
    function(){ $(this).css("color", "blue");}
)

谢谢。

I've just learnt a bit jQuery, and am trying to use it for a simple color-changing effect. Let's say I have two <div>s, #foo and #bar. #foo has a lot of URLs, and has the following CSS defined:

#foo a {color: blue; border-bottom: 1px dashed blue}
#foo a:hover {color: black; border-bottom: 1px solid black}

now I would like to change the color of the links (a:link) in #foo when the user clicks #bar, but keep the a:hover color untouched, so I write my function like this:

//...
$("#bar").click(function () {
  $("#foo a").css("color", "red");
});
//...

The problem is, while this function does change all the links into red, the a:hover color is lost, i.e. when a user moves the cursor on to the links, they will stay red, not turn black as I expected.

Since I see what jQuery does is put an inline style to <a>s within #foo, makes them into <a style="color:red;" href="...">, I guess this will overwrite the :hover pseudo-class. As the inline style attr for pseudo-class has not been implemented by anyone afaik, I doubt if I can get my intended effect at all...

still, is there any solutions that do not require me to write something like

$("#foo a").hover(
    function(){ $(this).css("color", "black");},
    function(){ $(this).css("color", "blue");}
)

?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浪菊怪哟 2024-07-20 00:25:30

摘自 Setting CSS Pseudo Class Rules From Javascript

您实际上可以更改悬停时或使用 :after 或您想要使用 javascript 的任何伪类的类的值:

$(document).ready(function()
{
    var left = (($('.selected').width() - 7) / 2) + 'px';
    document.styleSheets[1].insertRule('.selected:after { left: ' + left + '; }', 0);
    document.styleSheets[0].cssRules[0].style.left = left;
});

我希望这对任何人都有帮助。

Extracted from Setting CSS Pseudo Class Rules From Javascript

You actually can change the value of a class on hover or with :after or whatever pseudo-class you want with javascript:

$(document).ready(function()
{
    var left = (($('.selected').width() - 7) / 2) + 'px';
    document.styleSheets[1].insertRule('.selected:after { left: ' + left + '; }', 0);
    document.styleSheets[0].cssRules[0].style.left = left;
});

I hope this helps anyone.

柠檬 2024-07-20 00:25:30

!important 似乎使 css 属性比内联样式更强,至少在 Firefox 中是这样。 尝试将您的样式更改为如下所示:

#foo a:hover { color: black !important; }

!important seems to make css property stronger than inline style, at least in Firefox. Try to change your styles to something like this:

#foo a:hover { color: black !important; }
鹿童谣 2024-07-20 00:25:30

也许您可以删除当您将鼠标悬停在链接上时添加的类,如下所示:

$('#bar').click(function() {
  $('#foo a').css('color', 'red');
});
$('#foo').hover() {
  

[编辑]:您可能需要添加一个 IF 语句来首先查看该类是否存在。

#foo a').removeCss('color', 'red'); });

[编辑]:您可能需要添加一个 IF 语句来首先查看该类是否存在。

Maybe you could remove the class you added when you hover over the links, like this:

$('#bar').click(function() {
  $('#foo a').css('color', 'red');
});
$('#foo').hover() {
  

[EDIT]: You may need to add an IF statement to see if the class is there in the first place.

#foo a').removeCss('color', 'red'); });

[EDIT]: You may need to add an IF statement to see if the class is there in the first place.

傲世九天 2024-07-20 00:25:30

使用 Javascript 执行此操作的另一种令人讨厌的方法是清空容器元素并再次用内容填充它并再次设置单击事件。 这会破坏所有状态和事件,因此必须重新设置它们,但对于不包含大量数据的简单事物,它可以工作!

我将其用于使用 :hover 类的导航菜单。

$('#page_nav ul').click(clearNavMenu_Hover);

function clearNavMenu_Hover() {
    var tmpStore=$('#page_nav').html();
    $('#page_nav').empty();
    $('#page_nav').html(tmpStore);
    $('#page_nav ul').click(clearNavMenu_Hover);
}

Another nasty way of doing it using Javascript is to empty the container element and populate it again with the contents and setup the click event again. This destroys all states and events so they have to be setup again but for simple things that dont contain masses of data it works!

I use this for Nav menu's that use the :hover class.

$('#page_nav ul').click(clearNavMenu_Hover);

function clearNavMenu_Hover() {
    var tmpStore=$('#page_nav').html();
    $('#page_nav').empty();
    $('#page_nav').html(tmpStore);
    $('#page_nav ul').click(clearNavMenu_Hover);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文