Overview
Matches elements containing the given text
Parameters
textStringV1.1.4
A parameter to String to find
Example
Description:
Find all div elements containing "John"
HTML code:
<div>John Resig</div> <div>George Martin</div> <div>Malcom John Sinclair</div> <div>J. Ohn
jQuery code:
$("div:contains('John')")
Result:
[ <div>John Resig</div>, <div>Malcom John Sinclair</div> ]
##Instance code:Example 1:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("li:contains('html')").css("color","blue") }) }) </script> </head> <body> <ul> <li>html专区</li> <li>div+css专区</li> <li>Jquery专区</li> <li>Javascript专区</li> <li>html5专区</li> </ul> <button>点击查看效果</button> </body> </html>
The above code can set the text color in the li element containing "html" to blue.
:contains(text)
<body> <div> 我 </div><script> $(function(){ $(':contains(你)').css('background','lime'); });</script> </body>
$(function(){ $(':contains(你)').css('background','lime'); });
comment, it will also be ignored.
The above is the detailed content of JQuery :contains(text) selector use case. For more information, please follow other related articles on the PHP Chinese website!