在Jquery中的任何级别中查找元素类型的第一级子级

Finding first level child of a element type in any level in Jquery

本文关键字:一级 元素 任何级 Jquery 查找 类型      更新时间:2023-09-26

我试图在任何深度中找到元素的第一级子级。

例如,我有一个fieldset元素,它有一些子元素,包括其他字段集元素;我只想找到那些在第一个字段集中的元素,而不是在第二个字段集中。

换句话说,我想要父字段集的所有子项,但不想要任何嵌套字段集的子项。

给定此HTML:

<fieldset id='root'>
    <div>
       <div>
          <span>Test1</span>
          <span>Test2</span>
       </div>
       <span>Test3</span>
    </div>
    <fieldset>
       <div>
           <span>Test4</span>
           <span>Test5</span>
           <span>Test6</span>
       </div>
    </fieldset>
</fieldset>

我做$("#root").find("span"),它找到所有跨度,但我只想找到Test1Test2Test3

如何在jQuery中做到这一点?

我建议:

// select the elements you want to find,
// use filter() to filter out the elements you don't want:
$('span').filter(function() {
  // if the closest ancestor <fieldset> element to
  // the <span> you're looking for has the id of 'root'
  // this evaluates to true (is() returns a Boolean);
  // if the evaluation is true the current element is retained
  // in the collection, if false it's discarded:
  return $(this).closest('fieldset').is('#root');
// using css() to style the retained elements for verification:
}).css('color', 'red');

$('span').filter(function() {
  return $(this).closest('fieldset').is('#root');
}).css('color', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#" method="post">
  <fieldset id='root'>
    <div>
      <div>
        <span>Test1</span>
        <span>Test2</span>
      </div>
      <span>Test3</span>
    </div>
    <fieldset>
      <div>
        <span>Test4</span>
        <span>Test5</span>
        <span>Test6</span>
      </div>
    </fieldset>
  </fieldset>
</form>

参考文献:

  • closest()
  • css()
  • filter()
  • is()

您可以使用jQuery的filter()parents()函数来完成此操作。

你可以看看我的小提琴。

http://jsfiddle.net/ebilgin/9ov9kaaL/


编辑:未来使用的代码:

$("#root").find("span").filter( function () {
    if ( $(this).parents("fieldset").length ) {
        if ( $(this).parents("fieldset").parents("fieldset").length ) { 
            return false;
        }
        return true;
    }
}).css("color", "#CCC");

这些选择器中的任何一个都可以使用现有的HTML:

//selects spans of #root's first child:
$('#root > *:first span');  
//selects spans of #root's children that aren't fieldsets:
$('#root > :not(fieldset) span').css('background', 'yellow');

如果fieldset是第二个第一个子级,则第二个将起作用。

代码段:

$('#root > *:first span').css('color', 'red');
$('#root > :not(fieldset) span').css('background', 'yellow');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset id='root'>
  <div>
    <div>
      <span>Test1</span>
      <span>Test2</span>
    </div>
    <span>Test3</span>
  </div>
  <fieldset>
    <div>
      <span>Test4</span>
      <span>Test5</span>
      <span>Test6</span>
    </div>
  </fieldset>
</fieldset>

您可以在代码中使用css伪类:not和css类来过滤该div:

HTML

  <fieldset id='root'>
    <div>
       <div>
          <span>Test1</span>
          <span>Test2</span>
       </div>
       <span>Test3</span>
    </div>
    <fieldset>
       <div class="filter">
           <span>Test4</span>
           <span>Test5</span>
           <span>Test6</span>
       </div>
    </fieldset>

jQuery

$("#root div:not(.filter) span").css("color","red");


你可以在这里测试:

http://jsfiddle.net/w6k2z9r4/1/

var children = $('#root').children('div').children('span').css("background-color", "red" );

请参阅此jsbin:http://jsbin.com/yubafe/edit?html,js,控制台,输出