:contains() Selector

contains selector

version added: 1.1.4jQuery(':contains(text)')

描述: 选择所有包含指定文本的元素。

匹配的文本可以直接出现在所选的元素中,或在该元素的任何后代中,或两者兼有。正如属性值选择器,:contains()选择器中括号内的文字,可为空,或用引号包围。文本必须有匹配的情况下被选中。

Example:

Finds all divs containing "John" and underlines them.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  
<div>John Resig</div>

<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>
      
    
<script>
$("div:contains('John')").css("text-decoration", "underline");
    </script>

</body>
</html>

Demo:

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