:contains() 选择器
contains selector
描述:选择所有的包含指定的文本文件的元素。
加入于:1.1.4
jQuery( ":contains(text)" )text: 一个用来搜索的字符串。它是大小写敏感的。
这个匹配的文本可以直接在选中的元素内部出现,可以在元素的后代元素的任何位置,或者是两者的组合。作为一个元素属性值选择器,:contains()
括号中的文本可以写成一个裸露的单词,或者用引号包围起来。该文本必须是有匹配有匹配的情况下被选中。
示例
找到所有的包含了“John”的div,并给它们加下划线。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>contains demo</title> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div>John Resig</div> <div>George Martin</div> <div>Malcom John Sinclair</div> <div>J. Ohn</div> <script> $( "div:contains('John')" ).css( "text-decoration", "underline" ); </script> </body> </html>
演示结果