CSS Pseudo-classes with jQuery

佐手、 提交于 2019-12-09 01:21:33

问题


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.


回答1:


!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; }



回答2:


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.




回答3:


AFAIK, jQuery can't set pseudo classes. However, there's another way:

http://www.4pmp.com/2009/11/dynamic-css-pseudo-class-styles-with-jquery/




回答4:


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() {
  $'#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.




回答5:


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);
}


来源:https://stackoverflow.com/questions/486159/css-pseudo-classes-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!