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

jqueryweb developmentfront end technology

jQuery :contains() 选择器用于检查字符串是否包含子字符串。

在 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>

相关文章