定义和用法
:contains 选择器选取包含指定字符串的元素。
该字符串可以是直接包含在元素中的文本,或者被包含于子元素中。
经常与其他元素/选择器一起使用,来选择指定的组中包含指定文本的元素
语法
$(":contains(text)")
参数 | 描述 |
text | 必需。规定要查找的文本。 |
例子:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
p { font-size:20px; width:200px; cursor:default; color:#999; font-weight:bold; margin:0 10px; }
</style>
<script src="jquery-1.10.2.js"></script>
</head>
<body>
<div>My name is Donald ,
I live in Duckburg,
My best friend is Mickey.</div>
<script>
var newText = $("div").text().split(" ").join("</span> <span>");
$("div").html(newText)
.find(":contains('a')")
.css({"color":"#F00","font-style":"italic"});
</script>
</body>
</html>
效果图: