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
精彩评论