:contains(text)

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

jQuery(":contains(text)")

text: 用来查找的一个文本字符串。这是区分大小写的。

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

例子

查找所有包含“John”的div,并强调他们。

<!DOCTYPE html>
<html>
<head>
  <script src="./static/js/jquery-3.5.0.js"></script>
</head>
<body>
 
<p>John Resig</p> 
<p>George Martin</p>
<p>Malcom John Sinclair</p>
<p>J. Ohn</p>
 
<script>
$("p:contains('John')").css({"text-decoration":"underline","color":"#ff6600"});</script>
 
</body>
</html>

John Resig

George Martin

Malcom John Sinclair

J. Ohn

下篇: :empty