[英]How to delete an element which does not have a certain parent?
I want to delete list items who do not have a parent of ul
我想删除不用的父母谁列表项
ul
This is my HTML: 这是我的HTML:
<div class="yamm-content">
<div class="row">
<li> <a href="#">Should be deleted</a>
</li>
<li> <a href="#">Should be deleted</a>
</li>
<ul id="randomid" class="col-xs-12 col-sm-4">
<li> <a href="#">menu item</a>
</li>
<li> <a href="#">menu item</a>
</li>
<li> <a href="#">menu item</a>
</li>
</ul>
<ul id="randomid2" class="col-xs-12 col-sm-4">
<li> <a href="#">menu item</a>
</li>
<li> <a href="#">menu item</a>
</li>
<li> <a href="#">menu item</a>
</li>
</ul>
</div>
</div>
and my current script: 和我目前的脚本:
if (!$('.nav .yamm-content row li').closest('ul').length == 0) {
$(this).remove();
}
The first two list items don't have ul
's as parents. 前两个列表项没有
ul
作为父项。 So they should be deleted. 所以应该删除它们。
Simple: $('li:not(ul > li)').remove();
简单:
$('li:not(ul > li)').remove();
Demo http://jsfiddle.net/46NdQ/3/ 演示 http://jsfiddle.net/46NdQ/3/
$('li').filter(function(){return !$(this).closest('ul').length}).remove();
要么
$('li').not($('ul li')).remove();
Here i used Javascript only and it may take some lines of coding. 这里我只使用Javascript,它可能需要一些编码。 I suggest this may be useful some times when you don't want to go for jquery.
我建议当你不想去jquery时,这可能会有用。
var len = document.getElementsByTagName('li').length;
for (var i=0;i<len;i++)
{
if (document.getElementsByTagName('li')[i].parentNode.nodeName != "UL")
{
document.getElementsByTagName('li')[i].remove();
i--;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.