Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从选择中删除特定父级的子级_Javascript_Jquery - Fatal编程技术网

Javascript 从选择中删除特定父级的子级

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

下面是我的html的结构

<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*')
是否已尝试此方法?请检查此解决方案