jQuery :contains selector to search for multiple strings

后端 未结 4 1691
刺人心
刺人心 2020-11-27 12:26

Assuming i have:

  • Mary
  • John, Mary, Dave
  • John, Dave, Mary
  • 4条回答
    •  北荒
      北荒 (楼主)
      2020-11-27 13:14

      Answer

      To find li's that have text containing BOTH Mary AND John:

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

      To find li's that have text containing EITHER Mary OR John:

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

      Explanation

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

      $('li.one.two').      // Find 
    • 's with classes of BOTH one AND two $('li.one, li.two'). // Find
    • 's with a class of EITHER one OR two
    • It's the same with :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

    提交回复
    热议问题