简体   繁体   English

使用 jQuery 将类添加到 ul li 的每个级别

[英]Add class to each level of ul li using jQuery

I have this nested ul, which are parent -> child -> (grand)child levels.我有这个嵌套的 ul,它们是父级 -> 子级 ->(大)子级。

How can I use jQuery to spot every level adding a class to it so that I can style each level differently?我如何使用 jQuery 来发现每个级别向它添加一个类,以便我可以为每个级别设置不同的样式?

would be something like:会是这样的:

在此处输入图片说明

I couldn't do it with CSS because it needs to work fine at least on IE 7我不能用 CSS 做到这一点,因为它至少需要在 IE 7 上正常工作

<ul class="lista-regioes">
    <li class="cat-item cat-item-3 i-have-kids">
     <a href="http://localhost/poraidemochila/site/?local-destino=brasil" title="Ver todos os posts arquivados em Brasil">Brasil</a>
     <ul class="children">
        <li class="cat-item cat-item-13">
        <a href="#" title="#">Norte</a>
        </li>
        <li class="cat-item cat-item-4 i-have-kids">
        <a href="#" title="#">Sul</a>
        <ul class="children">
            <li class="cat-item cat-item-5">
                <a href="http://localhost/poraidemochila/site/?local-destino=parana" title="Ver todos os posts arquivados em Paraná">Paraná</a>
            </li>
        </ul>
    </li>
</ul>
    </li>
</ul>

the classes .cat-item and .cat-item-# are dynamically generated, so I can't use them in css the class .i-have-kids is added by the following js which I found here.cat-item.cat-item-#是动态生成的,所以我不能在 css 中使用它们类.i-have-kids是由我在这里找到的以下 js 添加的

 $('li.cat-item:has(ul.children)').addClass('i-have-kids');

but it does not really work since it just looks for elements that have children, and do not separate by levels, as you can see in the markup.但它并没有真正起作用,因为它只是寻找有子元素的元素,而不是按级别分隔,正如您在标记中看到的那样。

this.addClasses = function(parent, level) {
    // add a class to all <li> elements on this level:
    parent.children("li").addClass("cat-item-" + level);
    // find any child <ul> elements and run this function on them,
    // making sure to increment the level counter:
    if (parent.children("li").children("ul").length) {
        this.addClasses(parent.children("li").children("ul"), level + 1);
    }
};

// start off by selecting only the top-level <ul> elements:
this.addClasses($("body > ul"), 1);

Actually you do not need to classes to style your menu, you can access the levels in css with the > selector.实际上,您不需要类来设置菜单样式,您可以使用>选择器访问 css 中的级别。 That is the way I would probably do it.这就是我可能会这样做的方式。

But that is not the question, so I tried it in javascript.但这不是问题,所以我在 javascript 中尝试过。 What you want is a recursive function.你想要的是一个递归函数。

Something that would work like this:像这样工作的东西:

function addLevelClass($parent, level) {
    // add a parent class to the ul
    $parent.addClass('parent-'+level);
    // fetch all the li's that are direct children of the ul
    var $children = $parent.children('li');
    // add a child class to the li
    $children.addClass('child-'+level);
    // loop trough each li
    $children.each(function() {
        // get the ul that is a direct child of the li
        var $sublist = $(this).children('ul');
        // if an ul was found
        if ($sublist.length > 0) {
            // add a class to the current li indicating there is a sub list
            $(this).addClass('has-sublist');
            // repeat the process for the sublist, but with the level one higher
            // = recursive function call
            addLevelClass($sublist, level+1);
        }
    });
}

// call the function to add level classes on the upper most ul
addLevelClass($('.list'), 1);

There are probably more efficient ways to do this (js is not my strongpoint), but as an example I think it should work fine.可能有更有效的方法来做到这一点(js 不是我的强项),但作为一个例子,我认为它应该可以正常工作。 (Feel free to improve!) (随时改进!)

I set up a small example here: http://jsfiddle.net/Pevara/UPN33/我在这里设置了一个小例子: http : //jsfiddle.net/Pevara/UPN33/

Why not just do something like this: http://jsfiddle.net/Rpg88/为什么不做这样的事情: http : //jsfiddle.net/Rpg88/

HTML HTML

<ul>
    <li><a href="#">link</a></li>
    <li>
        <a href="#">link</a>
        <ul>
            <li><a href="#">link</a></li>
            <li>
                <a href="#">link</a>
                <ul>
                    <li><a href="#">link</a></li>
                    <li><a href="#">link</a></li>
                    <li><a href="#">link</a></li>
                </ul>
            </li>
            <li><a href="#">link</a></li>
        </ul>
    </li>
    <li><a href="#">link</a></li>
</ul>

CSS CSS

ul a { color:red; }
ul ul a {color:orange;}
ul ul ul a {color:lime;}

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

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