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>

輸出

這將產生以下輸出 −

更新於: 05-Nov-2019

423 次瀏覽

立即開始 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.