jQuery:包含用于搜索多个字符串的选择器

2023-12-03

假设我有:

<li id="1">Mary</li>
<li id="2">John, Mary, Dave</li>
<li id="3">John, Dave, Mary</li>
<li id="4">John</li>

如果我需要找到所有包含“John”和“Mary”的

  • 元素,我将如何构建 jQuery?
  • 搜索单个字符串似乎很容易:

    $('li:contains("John")').text()
    

    我正在寻找类似以下伪代码的内容:

    $('li:contains("John")' && 'li:contains("Mary")').text()
    

    Thanks!


    Answer

    To find li的文本包含 BOTH MaryAND John:

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

    To find li其文本包含 EITHER MaryOR John:

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

    解释

    只要想想:contains就好像它是一个类声明,就像.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
    

    Demo

    http://jsbin.com/ejuzi/edit

    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

    jQuery:包含用于搜索多个字符串的选择器 的相关文章

    随机推荐