0

I have a menu, I want to add class "current" to the <li> with id="home", so this is what I tried:

$('#main-nav ul li').attr('id').eq('home').addClass('current');

I also tried .is instead of .eq, non of them works!

How I could achieve this?

Thanks

4

4 回答 4

3
$('#main-nav ul li#home').addClass('current');

You can easily select for specific ids with the CSS id selector #. Depending on your DOM, you can even be more specific with your selectors, which will result in faster selection of the dom element:

$('#main-nav > ul > li#home').addClass('current');
于 2012-08-01T15:13:09.913 回答
1

You are using id, so you can use id selector, no need to use attr or eq:

$('#home').addClass('current');
于 2012-08-01T15:12:50.620 回答
1

You can specify the id of li which is home in your case in the same way you used this id main at the beginning of selector.

Try this,

$('#main-nav ul li#home').addClass('current');
于 2012-08-01T15:13:13.457 回答
0

Since you have an ID on the li element, you can just do this:

$('#home').addClass('current');
于 2012-08-01T15:13:15.780 回答