jQuery:包含搜索多个字符串的选择器 [英] jQuery :contains selector to search for multiple strings

查看:427
本文介绍了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()

谢谢!

推荐答案

answer



如下:

answer

It's like this:

$('li:contains("Mary"):contains("John")')



说明



$ c>:包含,好像它是一个类声明,如 .class

explanation

Just think of the :contains as if it was a class declaration, like .class:

$('li.one, li.two'). //you want either one OR two
$('li.one.two'). //you want BOTH on one element

// same with :contains
$('li:contains("mary"), li:contains("john")'). // either mary OR john
$('li:contains("mary"):li:contains("john")'). //li, which contains both



demo



http://jsbin.com/ejuzi/edit

这篇关于jQuery:包含搜索多个字符串的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆