我需要为每个ul设置深度。我做
$('.topnav').find('ul').each(function(){
$(this).attr('deep', $(this).index());
})
html
<ul class="topnav dropdown" >
<li class="expand">
<a href="/posluhy">Top</a>
<ul class="subnav">
<li class="expand">
<a href="/posluhy/metalocherepycja">Sub cat 1</a>
<ul class="subnav">
<li >
<a href="/posluhy/metalocherepycja/dsafsadf">Sub Sub cat 1</a>
</li>
</ul>
</li>
<li class="expand">
<a href="/posluhy/metalocherepycja">Sub cat 2</a>
<ul class="subnav">
<li >
<a href="/posluhy/metalocherepycja/dsafsadf">Sub Sub cat 2</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
但这会使每个ul deep ='1'。我想得到像
1
2
1
2
jQuery有可能吗?小提琴在这里http://jsfiddle.net/GLjZt/1/
索引不会提供您想要的内容,因为它只会告诉您元素在给定元素集中的位置;$(this).index()
将始终返回0。您需要根据父级的深度计算深度ul
:
$('.topnav').find('ul').each(function(){
$(this).attr('deep', $(this).parents('ul').length);//set attr deep
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句