jQuery :contains() 選擇器
jQuery 中的 :contain() 選擇器用於選擇包含指定文字的元素。
語法
語法如下 −
$(":contains(text)")此處,引數 text 是要查詢的文字。 具有指定文字的元素將被選定。
示例
下面我們看一個示例來實現 :contains() 選擇器 −
<!DOCTYPE html>
<html>
<head>
<style>
.one {
color: brown;
background-color: orange;
font-size: 16px;
border: 2px blue dashed;
}
</style>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
$(document).ready(function(){
$("p:contains(the)").addClass("one");
});
</script>
</head>
<body>
<h1>Examination</h1>
<p>Timings: 10AM - 1PM</p>
<p>The examination hall details would be emailed to the students.</p>
</body>
</html>輸出
這將產生以下輸出 −

廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP