如何檢查字串在 jQuery 中是否包含子字串?


jQuery :contains() 選擇器用於檢查字串是否在 jQuery 中包含子字串。

將你要在 contains() 方法中搜索的子字串設為如下:

$(document).ready(function(){
   $("p:contains(Video)").css("background-color", "blue");
});

現在,讓我們來看看完整的程式碼,檢查一個字串是否包含一個子字串:

示例

 線上演示

<!DOCTYPE html>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script>
      $(document).ready(function(){
         $("p:contains(Video)").css("background-color", "blue");
      });
   </script>
</head>
<body>

<h1>Tutorialspoint</h1>

<p>This is demo text.</p>
<p>Free Tutorials</p>
<p>Free Video Tutorials</p>

</body>
</html>

更新於: 20-06-2020

2K+ 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.