I have a multilevel menu like this :
我有一个像这样的多级菜单:
<ul>
<li>item 1
<ul>
<li>item 1.1</li>
<li>item 1.2</li>
</ul>
</li>
<li>item 2</li>
<li>item 3
<ul>
<li>item 3.1</li>
<li>item 3.2</li>
</ul>
</li>
<li>item 4</li>
</ul>
In jQuery I have
在jQuery我有
$("#divId ul li:odd").addClass("blue");
$("#divId ul li:even").addClass("green");
The problem is that jQuery adds the class to all the lists. I would like jQuery to just add the class to the first level (ul > li.class) and NOT the inner childs (ul > li > ul > li.class)
问题是jQuery将类添加到所有列表中。我希望jQuery只将类添加到第一级(ul> li.class)而不是内部子级(ul> li> ul> li.class)
Thanks
4 个解决方案
#1
3
This should work:
这应该工作:
$("#divId ul:first > li:odd").addClass("blue");
$("#divId ul:first > li:even").addClass("green");
It will add the classes to the children of the first ul tag found in divId.
它会将类添加到divId中找到的第一个ul标记的子级。
#2
2
you could try:
你可以尝试:
$("#divId ul li:not(#divId ul li ul li):odd").addClass("blue");
$("#divId ul li:not(#divId ul li ul li):even").addClass("green");
#3
1
I'm going to use assumptions here and you you know what they say about them ;)
我将在这里使用假设,你知道他们对他们的看法;)
If you want to add specific classes to all of the ul items then:
如果要为所有ul项添加特定类,则:
$('ul').addClass('class-addition');
and you can style the list items in the same way.
并且您可以以相同的方式设置列表项的样式。
If you want to add classes to the ul within a li you can use:
如果你想在li中添加类到ul,你可以使用:
$('li > ul').addClass('class-addition');
using the parent > child selectors.
使用父>子选择器。
I'd have a look at the selectors documentation provided by Branislav to find exactly what you want and use firebug to test the results though.
我将看看Branislav提供的选择器文档,以准确找到您想要的内容并使用firebug来测试结果。
#4
0
Can you, please, give more information on your case. From what I see you need simple select and add class. You can find information on jquery selectors here and on addClass method here
请您提供更多有关您案件的信息。从我看到你需要简单的选择和添加类。您可以在此处找到有关jquery选择器的信息,也可以在此处找到addClass方法
#1
3
This should work:
这应该工作:
$("#divId ul:first > li:odd").addClass("blue");
$("#divId ul:first > li:even").addClass("green");
It will add the classes to the children of the first ul tag found in divId.
它会将类添加到divId中找到的第一个ul标记的子级。
#2
2
you could try:
你可以尝试:
$("#divId ul li:not(#divId ul li ul li):odd").addClass("blue");
$("#divId ul li:not(#divId ul li ul li):even").addClass("green");
#3
1
I'm going to use assumptions here and you you know what they say about them ;)
我将在这里使用假设,你知道他们对他们的看法;)
If you want to add specific classes to all of the ul items then:
如果要为所有ul项添加特定类,则:
$('ul').addClass('class-addition');
and you can style the list items in the same way.
并且您可以以相同的方式设置列表项的样式。
If you want to add classes to the ul within a li you can use:
如果你想在li中添加类到ul,你可以使用:
$('li > ul').addClass('class-addition');
using the parent > child selectors.
使用父>子选择器。
I'd have a look at the selectors documentation provided by Branislav to find exactly what you want and use firebug to test the results though.
我将看看Branislav提供的选择器文档,以准确找到您想要的内容并使用firebug来测试结果。
#4
0
Can you, please, give more information on your case. From what I see you need simple select and add class. You can find information on jquery selectors here and on addClass method here
请您提供更多有关您案件的信息。从我看到你需要简单的选择和添加类。您可以在此处找到有关jquery选择器的信息,也可以在此处找到addClass方法