我有一个菜单项列表,其中包含子uls和sub-sub uls。 对于子 uls,我想仅在父级有子级时才从父级中删除 href。
这就是我到目前为止所拥有的,不太确定如何将其转换为 if -> else 语句。
$('#menu-sidebar-menu > li > ul:has(li) > a').children(['li']).find('a:first').removeAttr('href');
<li class="cat-item cat-item-89"><a href="#" title="Detailed posts for products.">Products</a>
<ul class='children'>
<li class="cat-item cat-item-100"><a href="#" title="Detailed posts for products.">ALL</a>
</li>
<li class="cat-item cat-item-94"><a href="#" title="Post about products for Ducati sportbikes.">Ducati</a>
</li>
<li class="cat-item cat-item-91"><a href="#" title="Posts about products for Honda sportbikes.">Honda</a>
<ul class='children'>
<li class="cat-item cat-item-104"><a href="#" title="testing">CBR 1000RR</a>
</li>
<li class="cat-item cat-item-103"><a href="#" title="TESTING">CBR 600RR</a>
</li>
</ul>
</li>
<li class="cat-item cat-item-93"><a href="#" title="Post about products for Kawasaki sportbikes.">Kawasaki</a>
非常感谢您的任何意见。
塞吉奥
像这样的东西?
$.each($("ul > li"), function (index, obj) {
if ($(obj).has("ul").length) {
$(obj).has("ul").find("a:first").removeAttr("href");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="cat-item cat-item-89"><a href="#" title="Detailed posts for products.">Products</a>
<ul class='children'>
<li class="cat-item cat-item-100"><a href="#" title="Detailed posts for products.">ALL</a>
</li>
<li class="cat-item cat-item-94"><a href="#" title="Post about products for Ducati sportbikes.">Ducati</a>
</li>
<li class="cat-item cat-item-91"><a href="#" title="Posts about products for Honda sportbikes.">Honda</a>
<ul class='children'>
<li class="cat-item cat-item-104"><a href="#" title="testing">CBR 1000RR</a>
</li>
<li class="cat-item cat-item-103"><a href="#" title="TESTING">CBR 600RR</a>
</li>
</ul>
</li>
<li class="cat-item cat-item-93"><a href="#" title="Post about products for Kawasaki sportbikes.">Kawasaki</a>
</li>
</ul>