jquery manipulate a tags with .css()
I need to change the font color of a div with the id name "nav" to white.
I did:
$("#nav").css("color","white");
This works for all the text that isn't wrapped in < a > tags but I need those changed too.
I tried adding:
$("a").css("color","white");
But that doesn't work. I also tried:
var changeAColor = document.g开发者_JS百科etElementsByTagName("a")
$(changeAColor).css("color","white");
Any ideas appreciated.
$('#nav, #nav a').css('color', 'white');
selects all the direct and indirect anchor tags in the #nav div.
$('#nav, #nav > a').css('color', 'white');
selects all the direct anchor tags in the #nav div.
The reason why $("#nav").css("color","white");
does not change the color of the links is because the selector in your stylesheet that styles the a
tags is more specific than the inline style being applied to the div tag. You will notice the same effect if you were to add the following rule to your stylesheet, the links will not change: #nav { color: white }
That being said, the jQuery statement to change all of the text, including the links would be $('#nav, #nav a').css('color', 'white');
The only way I was able to reproduce your problem was if I had a style applied to a
tags that used the important
declaration, as in: a { color: #0000ff!important; }
That seems like a shot in the dark, but that's all I can think of for now...
精彩评论