繁体   English   中英

从选择中移除特定父母的子女

[英]remove children of particular parent from selection

下面是我的html的结构

<div class="parent">
//many other elements
<div class="child">
<div class="grandchild"></div>

</div>
</div>

我想选择父级内的所有元素,除了子元素内的元素

我尝试了下面的一个没有用的

$(".parent *:not(.child *)")

我该如何选择?

尝试这个:

$('.parent *').filter(function(){
      return $(this).parents('.child').length === 0;
});

这将选择.parent所有元素,除了那些也是.parent子/子子的.child

$('.parent *:not(.child, .child *)')

尝试这个。

虽然也许你想要这个:

$(".parent > *:not(.child)")

试试这个:将选择以父类.parent下的class child开头的所有div。

$('.parent div[class^="child"]')
$('.parent *').not('.child *')

我想选择父级内的所有元素,除了子元素内的元素

你可以使用find方法。 它会找到除了班级child里面的孩子以外的所有后代/ child

$(".parent").find("*").not($(".child").children()).each(function(){
alert($(this).prop("class"));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM