[英]Add different classes to list item parents using jquery
I have a list with different levels of depth: 我有一个深度不同的列表:
<ul>
<li>Item 1</li>
<li>Item 2
<ul class="sub-menu">
<li>Sub item 1</li>
<li>Sub item 2</li>
<li>Sub item 3
<ul class="sub-menu">
<li>Subsub item 1</li>
<li>Subsub item 2</li>
</ul>
</li>
<li>Sub item 4</li>
</ul>
</li>
<li>Item 3</li>
</ul>
I am using the following jquery script to add a class to the parents: 我正在使用以下jquery脚本向父级添加一个类:
$("ul li ul").parent().addClass("menuparent");
Is there a way to add this class only to the top level parent li
's and a different class for all other (deeper) parent li
's? 有没有办法来这个类仅添加到顶层父li
的和不同类所有其他(深)父li
的?
You could run a closest check to see if there are any parents that are li. 您可以进行最接近的检查,看看是否有父母是li。
if ($element.closest("li").length === 0) {
$element.addClass("topLvl");
} else {
$element.addClass("innerLvl");
}
Here is one way to do it. 这是一种方法。
$("ul li ul").parent().addClass("otherclass");
$("ul li >ul").parent().removeClass("otherclass").addClass("menuparent");
Try: 尝试:
$("ul:first>li").addClass("menuparent")
.find('li>ul').parent().addClass('otherparent');
Here's a recursive solution: 这是一个递归解决方案:
function markNestedLists(par, level){
par.addClass("level-" + level);
par.children("li").children("ul").each(function(){
markNestedLists($(this), level + 1);
});
}
markNestedLists($("ul").first(), 1);
http://jsfiddle.net/h9xvY/1/ http://jsfiddle.net/h9xvY/1/
If you know the ID of the parent of the topmost UL you could use it like so: 如果您知道最高UL的父代的ID,则可以这样使用它:
markNestedLists($("#myParent > ul"), 1);
Depending on what you want the names of the classes to be, I'd use this: 根据您想要的类名称而定,我将使用以下代码:
$(function(){
$('ul li').addClass(function(){
return 'depth-' + $(this).parents('ul').length;
});
});
You can see a working example here: http://jsfiddle.net/russelluresti/3peCS/ 您可以在此处看到一个有效的示例: http : //jsfiddle.net/russelluresti/3peCS/
If you want special class names, and not number additions, you'd have to run a switch statement. 如果您想要特殊的类名,而不是附加的数字,则必须运行switch语句。 But the concept is the same. 但是概念是相同的。 Use a function inside addClass
to determine the depth (by using parents()
) and return the appropriate value. 使用addClass
内部的函数确定深度(通过使用parents()
)并返回适当的值。
Ideally, there's some absolute reference like an ID or other searchable attribute that is unique to the top-most ul
, but you can work around that. 理想情况下,有一些绝对引用,例如ID或其他可搜索属性,这对于最顶端的ul
是唯一的,但是您可以解决此问题。 Either way, the important thing is the child selector: >
instead of the implicit descendant selector. 无论哪种方式,重要的是子选择器: >
而不是隐式后代选择器。 It will specify that you only want to find ul
's that are exactly so many levels below that top-level element. 它会指定您只想查找位于该顶级元素下这么多个ul
。
When, in your base case, you use: 在基本情况下,何时使用:
$("ul li ul").parent()
you get all ul
's that are any descendant (children, grandchildren, great-grandchildren...) of any li
's, that are descendant of any ul
's. 你得到所有ul
的是任何后代任何的(子女,孙子,曾孙......) li
的,这是任何后代ul
的。 Instead, you'd use: 相反,您将使用:
$("ul#topmost > li > ul").parent()
which gets you only a ul
that is the child of an li
that is a child of the specific ul
at the top of the tree. 这只会让您得到一个ul
,它是li
的孩子 ,而li
则是树顶部特定ul
的孩子 。
If you don't have an id or other explicit selector for the top of the tree, the top-level ul
must itself be a child of either a div
or body
or some other block-level element. 如果树的顶部没有id或其他显式选择器,则顶级ul
本身必须是div
或body
或其他某些块级元素的子级。 So, you can clearly and distinctly get the hierarchy you want by just adding that parent of the top-level ul
: 因此,您只需添加顶级ul
父级,就可以清楚而明确地获得所需的层次结构:
$("body > ul > li > ul").parent()
Also: I forgot that you also wanted to be able to select the other parent li
's that aren't captured by the selector above. 另外:我忘记了您还希望能够选择上面的选择器未捕获的其他父li
。 You can do that using the :not
selector, or JQuery's .not()
method, like so: 您可以使用:not
选择器或JQuery的.not()
方法执行此操作,如下所示:
$("li>ul:not(body > ul > li > ul)").parent()
To combine the two lines into one, you'd first add the deeperParent
class to all such li
's, then filter for the top-level parent, and assign menuParent
only there: 要将这两行合并为一,您需要先将deeperParent
类添加到所有此类li
,然后过滤顶级父级,然后仅在其中分配menuParent
:
$("li>ul").parent().addClass('deeperParent').filter('body > ul > li').
removeClass('deeperParent').addClass('menuParent')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.