.find()

.find( selector ) 官网原E文: jQuery

描述: 获得当前元素匹配集合中每个元素的后代,选择性筛选的选择器。

  • version added: 1.0.find( selector )

    selector一个用于匹配元素的选择器字符串。

  • version added: 1.6.find( jQuery object )

    jQuery object一个用于匹配元素的jQuery对象。

  • version added: 1.6.find( element )

    element一个用于匹配元素的DOM元素。

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

该方法选择性地接受同一类型选择表达,我们可以传递给$()函数。如果紧随兄弟匹配选择器,它在新建成的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');

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

Examples:

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

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.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>

Demo:

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

<!DOCTYPE html>
<html>
<head>
  <style>
    span { color: blue; }
  </style>
  <script src="http://code.jquery.com/jquery-git.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>

Demo:

Example: Add spans around each word then add a hover and italicize words with the letter 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="http://code.jquery.com/jquery-git.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>

Demo:

jQuery 1.6 API 中文版Clove整理、修订