开发者

How to use jQuery to select this parent?

开发者 https://www.devze.com 2023-01-19 18:48 出处:网络
I have a recursive menu where I need the children items to effect the parent. In my example below, how do I add a \"selected\" class if any of the children have a \"selected\" class?

I have a recursive menu where I need the children items to effect the parent. In my example below, how do I add a "selected" class if any of the children have a "selected" class?

<ul>
    <li class="administration first">
        <a href="/administration.aspx"><span>Administration</span></a>
        <ul>
            <li class="users first selected"><a href="/administration/users.aspx"><span>Users</span></a></li>
            <li class="forms last"><a href="/administration/forms.aspx"><span>Forms</span></a></li>
        </ul>
        <div style="clear: both;"></div>
    </li>
    <li class="analytics"><a href="/analytics.aspx"><span>Analytics</span></a></li>
    <li class="options"><a href="/options.aspx"><span>Options</span></a></li>
    <li class="system last"><a href="开发者_Python百科/system.aspx"><span>System</span></a></li>
</ul>


Use parents method to filter parent list elements.

$('li.selected').parents('li').addClass('selected');

An example

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号