我有这个HTML结构:
<ul>
<li class="cat-item">
<a href="link.html">Link</a>
</li>
<li class="subcat-item" style="display: none">
<ul class="subcat-list">
</ul>
</li>
<li class="cat-item">
<a href="link2.html">Link2</a>
</li>
<li class="subcat-item" style="display: none">
<ul class="subcat-list">
<li class="subcat-list-item"><a href="link3.html">Link 3</a></li>
<li class="subcat-list-item"><a href="link4.html">Link 4</a></li>
<li class="subcat-list-item"><a href="link5.html">Link 5</a></li>
</ul>
</li>
</ul>
现在我想做的是:
如果(li.cat-item后面的下一个元素,class.subcat-item有子ul.subcat-list,而这个ul.subcat-list有子元素)单击此.cat item时,li.subcat-item滑动并返回false(页面不重新加载)
否则,当您单击此.cat项时,会出现函数:window.location=this.ref;
所以很简单:如果category有子类别slideToggle列表和子类别链接。
如果类别没有子类别,请转到类别链接。
有什么帮助吗?
//编辑
谢谢你的回答。又走了一步,我受不了。这里是HTML:
<ul>
<li class="cat-item">
<a href="link.html">Link</a>
</li> <!-- cat-item -->
<li class="subcat-item" style="display: none;">
<ul class="subcat-list">
</ul> <!-- subcat-list -->
</li> <!-- subcat-item -->
<li class="cat-item">
<a href="link.html">Link</a>
</li> <!-- cat-item -->
<li class="subcat-item" style="display: none;">
<ul class="subcat-list">
<li class="subcat-list-item">
<a href="link.html">Link</a>
</li> <!-- subcat-list-item -->
<li class="sub-subcat-item" style="display: none;">
<ul class="sub-subcat-list">
</ul> <!-- sub-subcat-list -->
</li> <!-- sub-subcat-item -->
<li class="subcat-list-item">
<a href="link.html">Link</a>
</li> <!-- subcat-list-item -->
<li class="sub-subcat-item" style="display: none;">
<ul class="sub-subcat-list">
<li class="sub-subcat-list-item">
<a href="link.html">Link</a>
</li> <!-- sub-subcat-list-item -->
<li class="sub-subcat-list-item">
<a href="link.html">Link</a>
</li> <!-- sub-subcat-list-item -->
</ul> <!-- sub-subcat-list -->
</li> <!-- sub-subcat-item -->
</ul> <!-- subcat-list -->
</li> <!-- subcat-item -->
</ul>
所以现在:当用户单击.subcat列表项和下一个带有class.subcat项的元素没有子元素(ul没有li)时,他应该转到.subcat列表项url。否则,子子子项应滑动切换。
最佳答案
尝试
$('.cat-item').click(function (e) {
var x = $(this).next('.subcat-item').children('.subcat-list').children('.subcat-list-item');
console.log(x.length);
if (x.length > 1) {
e.preventDefault();
x.closest('.subcat-item').slideToggle();
}
});
DEMO
Op更新问题后更新
$('.subcat-list-item').click(function (e) {
var x = $(this).next('.sub-subcat-item').children('ul').children('li');
console.log(x.length);
if (x.length != 0) {
e.preventDefault();
$(this).next('.sub-subcat-item').slideToggle();
}
});
DEMO
关于jquery - jQuery,如果下一个隐藏元素的宽度类没有子代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18549542/