Javascript/jQuery - 每个跳过所有 children

Javascript/jQuery - Each skip all children

我正在获取文档中的所有元素:

$("*").each(function(){
   var el = $(this);
});

我需要的是获取除 children 元素之外的所有元素。我的意思是:

<div> <!--GET IT-->
   <div></div> <!--DON'T GET IT-->
</div>

<div> <!--GET IT-->
   <div></div> <!--DON'T GET IT-->
   <label></label> <!--DON'T GET IT-->
</div>

我该如何过滤?

您可以定位最外层 div 元素的父元素,然后使用直接子选择器获取第一级子元素。

让我们假设您在正文容器中有上述标记。然后你可以使用

$('body >  *').each(function(){
  var el = $(this);
});

你可以使用这个

 $('body > * ').each();

这针对 body 的所有子元素,但不针对元素的子元素。

看这个:https://api.jquery.com/child-selector/



$("parent > child").each(function(){

    $(this) /* children */

});