我有一个包含子uls和子子菜单的菜单项列表。对于子子,我只想在父母有孩子的情况下从父母那里删除href。
这是我到目前为止所做的,并不确定如何将其转换为if - >其他陈述。
$('#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>
非常感谢您的任何意见。
塞尔吉奥
答案 0 :(得分:2)
这样的东西?
$.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>