Javascript 从选择中删除特定父级的子级
下面是我的html的结构Javascript 从选择中删除特定父级的子级,javascript,jquery,Javascript,Jquery,下面是我的html的结构 <div class="parent"> //many other elements <div class="child"> <div class="grandchild"></div> </div> </div> 我如何选择它?试试这个: $('.parent *').filter(function(){ return $(this).parents('.child').lengt
<div class="parent">
//many other elements
<div class="child">
<div class="grandchild"></div>
</div>
</div>
我如何选择它?试试这个:
$('.parent *').filter(function(){
return $(this).parents('.child').length === 0;
});
这将选择.parent
中的所有元素,但同时也是.child
$('.parent *:not(.child, .child *)')
$(".parent").find("*").not($(".child").children()).each(function(){
alert($(this).prop("class"));
});
试试这个
尽管你可能想要这个:
$(".parent > *:not(.child)")
试试这个:
将选择父类.parent下以类子级开头的所有div
$('.parent div[class^="child"]')
我想选择父级中的所有元素,但不包括父级中的元素
孩子
您可以使用find方法。
它将找到除类child
$('.parent *:not(.child, .child *)')
$(".parent").find("*").not($(".child").children()).each(function(){
alert($(this).prop("class"));
});
$('.parent*')
是否已尝试此方法?请检查此解决方案