:has() Selector

has selector

version added: 1.1.4jQuery(':has(selector)')

描述: 选择含有选择器所匹配的至少一个元素的元素。

表达式 $('div:has(p)') 匹配一个 <div>如果<p>在其后代中存在的任何地方,不仅是一种直接的子元素。

Example:

Adds the class "test" to all divs that have a paragraph inside of them.

<!DOCTYPE html>
<html>
<head>
  <style>
  .test{ border: 3px inset red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <div><p>Hello in a paragraph</p></div>

  <div>Hello again! (with no paragraph)</div>
<script>$("div:has(p)").addClass("test");</script>

</body>
</html>

Demo:

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