简体   繁体   English

jQuery从UL层次结构中删除类,除了

[英]jQuery remove class from UL hierarchy except for

See fiddle: http://jsfiddle.net/3mpire/yTzGA/1/ 请参阅小提琴: http//jsfiddle.net/3mpire/yTzGA/1/

Using jQuery how can I remove the "active" class from all LIs except for the one furthest (deepest) from the root? 使用jQuery如何从所有LI中删除“活动”类除了从根最远(最深)的那个?

<div class="navpole">
    <ul>
        <li class="active"><a href="#">Lorem ipsum</a></li>
        <li><a href="#">Lorem ipsum</a>
            <ul>
                <li class="active"><a href="#">Lorem ipsum</a></li>
                <li><a href="#">Lorem ipsum</a></li>
                <li><a href="#">Lorem ipsum</a>
                     <ul>
                          <li class="active"><a href="#">Lorem ipsum</a></li>
                          <li><a href="#">Lorem ipsum</a></li>
                     </ul>
                </li>
                <li><a href="#">Lorem ipsum</a></li>
            </ul>
        </li>
        <li><a href="#">Lorem ipsum</a></li>
        <li><a href="#">Lorem ipsum</a></li>
    </ul>
</div>

This is the desired result: 这是期望的结果:

<div class="navpole">
    <ul>
        <li><a href="#">Lorem ipsum</a></li>
        <li><a href="#">Lorem ipsum</a>
            <ul>
                <li><a href="#">Lorem ipsum</a></li>
                <li><a href="#">Lorem ipsum</a></li>
                <li><a href="#">Lorem ipsum</a>
                     <ul>
                          <li class="active"><a href="#">Lorem ipsum</a></li>
                          <li><a href="#">Lorem ipsum</a></li>
                     </ul>
                </li>
                <li><a href="#">Lorem ipsum</a></li>
            </ul>
        </li>
        <li><a href="#">Lorem ipsum</a></li>
        <li><a href="#">Lorem ipsum</a></li>
    </ul>
</div>

Sort them by number of parents and exclude the last one as that will be the one nested the furthest regardless of markup, and remove the class on the rest of them 按父母的数量对它们进行排序并排除最后一个,因为无论标记如何都将嵌套到最远的那个,并删除其余部分的类

$('.active').sort(function(a,b) {
    return $(a).parents().length > $(b).parents().length;
}).not(':last').removeClass('active');

FIDDLE 小提琴

To do it for each navpole, just wrap it in a each() 要为每个navpole执行此操作,只需将其包装在each()

$('.navpole').each(function() {
    $('.active', this).sort(function(a,b) {
        return $(a).parents().length > $(b).parents().length;
    }).not(':last').removeClass('active');
});

FIDDLE 小提琴

Note: This assumes that there is only a single path from the root to the deepest .active element, which contains all .active Elements under that root (ie no branching). 注意:这假设从根到最深的.active元素只有一条路径,它包含该根下的所有 .active元素(即没有分支)。 If that cannot be guaranteed, then this solution won't work. 如果无法保证,那么此解决方案将无效。

$('.navpole .active').slice(0, -1).removeClass('active');

Since the order of the selected elements is the order in which their appear in the document, the "deepest" element is inevitably the last one, so we have to remove the class from every selected element, but the last one. 由于所选元素的顺序是它们在文档中出现的顺序,因此“最深”元素不可避免地是最后一个元素,因此我们必须从每个选定元素中删除该类,但最后一个元素。

DEMO DEMO

You could use each() to loop through the .active elements and remove the class if its sibling elements have a ul child: 您可以使用each()循环遍历.active元素,并在其兄弟元素具有ul子元素时删除该类:

UPDATED EXAMPLE HERE 这里有更新的例子

$('.navpole .active').each(function(){
    if($(this).siblings().children('ul').length > 0 ){
        $(this).removeClass('active');
    }
});

If you want to keep just the last .active item into each .navpole you can do something like this: 如果你想将最后一个.active项保存到每个.navpole你可以这样做:

$(document).ready(function () {
    //For each navpole
    $('.navpole').each(function(){
    //Find number of active elements
        var len = $('.active',this).length;
    //Filter that elements and remove class for all elements except the last one
        $('.active',this).filter(function(index){
        return index != len-1;
        }).removeClass('active');
    }) 
});

Check this Demo Fiddle 检查这个演示小提琴

Try this out. 试试吧。 The previous example by adeneo is great, but has issues if you have two active nodes at the same level at the lowest level of the hierarchy and want all of their active states preserved. adeneo的前一个示例很棒,但如果在层次结构的最低级别有两个活动节点并且希望保留所有活动状态,则会出现问题。 This version will keep all lowest nodes selected if they're at the same level in the hierarchy. 如果它们在层次结构中处于同一级别,则此版本将保留所有最低节点。

$(document).ready(function () {
    $('.navpole').each(function () {
        var active = $(this).find(".active").map(function () {
            return $(this).parents().length;
        });
        var maxParents = Math.max.apply(Math, active);
        $(this).find(".active").each(function () {
            if ($(this).parents().length != maxParents) $(this).removeClass("active");
        });
    });
});

here is a fiddle to illustrate: http://jsfiddle.net/CGcr9/1/ 这里有一个小提琴来说明: http//jsfiddle.net/CGcr9/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM