• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • find()

    通过一个选择器,jQuery对象,或元素过滤,得到当前匹配的元素集合中每个元素的后代。

    .find(selector)
    • selector类型: Selector。一个用于匹配元素的选择器字符串。
    .find(element)
    • element类型:Element 或 jQuery。一个元素或一个jQury对象用来匹配元素。

    如果一个jQuery对象表示一个DOM元素的集合,.find()方法允许我们能够通过查找DOM树中的这些元素的后代元素,匹配的元素将构造一个新的jQuery对象。.find().children()方法是相似的,但后者只是再DOM树中向下遍历一个层级(注:就是只查找子元素,而不是后代元素)。

    .find()方法还可以接受一个选择器表达式,该选择器表达式可以是任何可传给$()函数的选择器表达式。如果紧随兄弟匹配选择器,它将被保留在新构建的jQuery对象中;否则,它被排除在外。这个表达式允许的包括类似> p的选择器,它会找到所有的段落,这些段落是jQuery对象中元素的子元素。

    考虑一个页面上的一个简单的列表:

    <ul class="level-1">
      <li class="item-i">I</li>
      <li class="item-ii">II
        <ul class="level-2">
          <li class="item-a">A</li>
          <li class="item-b">B
            <ul class="level-3">
              <li class="item-1">1</li>
              <li class="item-2">2</li>
              <li class="item-3">3</li>
            </ul>
          </li>
          <li class="item-c">C</li>
        </ul>
      </li>
      <li class="item-iii">III</li>
    </ul>
    

    如果我们从item II 开始,我们可以找到它里面的清单项目:

    $('li.item-ii').find('li').css('background-color', 'red');
    

    该调用的结果是II项的A,B,1,2,3,和C的背景变为红色,尽管item II匹配选择表达式,它不包括在结果中;只有它的后代被认为是匹配的候选元素。

    和其他的遍历方法不同,调用.find()需要选择器表达式参数。如果我们需要检索所有的后代元素,我们可以通过在普遍选择器'*'来完成。

    选择器上下文通过.find()方法实现,$('li.item-ii').find('li')等价于$('li','li.item-ii').

    在 jQuery 1.6中,我们还可以用给定的jQuery集合或元素来过滤元素。在如上所述相同的嵌套列表中,如果我们开始:

    var $allListElements = $('li');
    

    然后通过这个jQuery对象查找:

    $('li.item-ii').find( $allListElements );
    

    这将返回一个只包含列表元素的jQuery集合,它们是项目II的后代。

    同样,一个元素可能也可以通过查找:

    var item1 = $('li.item-1')[0];
    $('li.item-ii').find( item1 ).css('background-color', 'red');
    

    该调用的结果将是对item 1 的背景替换为红色。

    例子

    开始搜索段落所有后代的span元素,和$("p span")一样

    <!DOCTYPE html>
    <html>
    <head>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
    </head>
    <body>
      <p><span>Hello</span>, how are you?</p>
    <p>Me? I'm <span>good</span>.</p>
    <script>
      $("p").find("span").css('color','red');
    </script>
     
    </body>
    </html>
    

    Hello, how are you?

    Me? I'm good.

    一个选择使用的所有span标签jQuery集合。只有spans在P标签更改为红色,而另一些是蓝色。

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        span { color: blue; }
      </style>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
    </head>
    <body>
      <p><span>Hello</span>, how are you?</p>
      <p>Me? I'm <span>good</span>.</p>
      <div>Did you <span>eat</span> yet?</div>
    <script>
      var $spans = $('span');
      $("p").find( $spans ).css('color','red');
    </script>
     
    </body>
    </html>
    

    Hello, how are you?

    Me? I'm good.

    Did you eat yet?

    为每个单词添加 span 标签,并为 span 标签添加 hover 事件,并且将含有 t 的单词变为斜体。

    <!DOCTYPE html>
    <html>
    <head>
      <style>
      p { font-size:20px; width:200px; cursor:default; 
          color:blue; font-weight:bold; margin:0 10px; }
      .hilite { background:yellow; }
      </style>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
    </head>
    <body>
      <p>
      When the day is short
      find that which matters to you
      or stop believing
      </p>
    <script>
      var newText = $("p").text().split(" ").join("</span> <span>");
      newText = "<span>" + newText + "</span>";
     
      $("p").html( newText )
        .find('span')
        .hover(function() { 
          $(this).addClass("hilite"); 
        },
          function() { $(this).removeClass("hilite"); 
        })
      .end()
        .find(":contains('t')")
        .css({"font-style":"italic", "font-weight":"bolder"});
     
    </script>
     
    </body>
    </html>
    

    When the day is short find that which matters to you or stop believing

    上篇:children()

    下篇:parent()