jQuery:包含选择器来搜索多个字符串 [英] jQuery :contains selector to search for multiple strings
本文介绍了jQuery:包含选择器来搜索多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有:
<li id="1">Mary</li>
<li id="2">John, Mary, Dave</li>
<li id="3">John, Dave, Mary</li>
<li id="4">John</li>
如果我需要找到所有的<li>包含John"和Mary"的元素,我将如何构造jQuery?
If i need to find all <li> Elements which contain "John" and "Mary", how would i construct the jQuery?
搜索单个字符串似乎很容易:
A search for a single string seems easy:
$('li:contains("John")').text()
我正在寻找类似下面的伪代码:
I am looking for something like the following pseudo code:
$('li:contains("John")' && 'li:contains("Mary")').text()
谢谢!
推荐答案
答案
要查找 li
的文本包含 BOTH Mary AND John:
Answer
To find li
's that have text containing BOTH Mary AND John:
$('li:contains("Mary"):contains("John")')
要查找具有包含 Mary OR John 的文本的 li
:
To find li
's that have text containing EITHER Mary OR John:
$('li:contains("Mary"), li:contains("John")')
说明
把 :contains
想象成一个类声明,比如 .class
:
Explanation
Just think of the :contains
as if it was a class declaration, like .class
:
$('li.one.two'). // Find <li>'s with classes of BOTH one AND two
$('li.one, li.two'). // Find <li>'s with a class of EITHER one OR two
:contains
也是一样的:
$('li:contains("Mary"):contains("John")'). // Both Mary AND John
$('li:contains("Mary"), li:contains("John")'). // Either Mary OR John
演示
这篇关于jQuery:包含选择器来搜索多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文