jQuery: getting level of selected li
i'm working with wordpress+jQuery - there's a navigationscript (it's an UL) with several levels like this:
<ul>
<li>level 1 - item1</li>
<li>level 1 - item2
<ul>
<li>level 2 - item1</li>
<li class=current_page_item>level 2 - item开发者_开发知识库2</li>
</ul>
</li>
<li>level 1 - item3</li>
</ul>
wordpress is applying a css-class called current_page_item to the current LI. my question: when selecting that LI - how can i find out it's level in the UL? (in my case: level 2)
By counting its parents:
$('.current_page_item').parents('ul').length;
var selectedItem = $('.current_page_item');
var Index = $("li", "ul").index(selectedItem);
Index gives you the index of that class..
精彩评论