×

学习下JS里的:contains()选择器的应用

作者:Terry2019.11.26来源:Web前端之家浏览:5693评论:0
关键词:js:contains()

jQuery中:contains()选择器用于选择包含指定字符串的元素。该字符串可以是直接包含在元素中的文本,或者被包含于子元素中。该选择器经常与其他元素/选择器一起使用,来选择指定的组中包含指定文本的元素。

语法:

$(":contains(text)")

参数:该:contains()选择器包含单个参数文本,该文本是必需的,用于指定要查找的文本。

示例1:使用:contains()选择器来选择元素

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
		<script> 
	        $(document).ready(function(){
			 	$("h3:contains(水果)").css("background-color","yellow");
			});
      	</script>
	</head>
	<body style="text-align:center;">
         <h2>jQuery :contains()选择器</h2> 
         <h3>以下最喜欢的水果是什么:</h3> 
           
         <div id="choose"> 
             <h3>苹果</h3> 
             <h3>香蕉</h3> 
             <h3>桃子</h3> 
         </div> 
	</body>
</html>

示例2:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p:contains(is)").css("background-color","yellow");
});
</script>
</head>
<body>

<h1>欢迎来到这里!</h1>
<p class="intro">My name is Donald.</p>
<p>I live in Duckburg.</p>
<p>My best friend is Mickey.</p>
</body>
</html>

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/js8uerioew32984230984.html

网友评论文明上网理性发言 已有0人参与

发表评论: