jQuery :contains(), but to match an exact string

后端 未结 4 1752
别跟我提以往
别跟我提以往 2020-11-30 07:11

As said in the title, I\'d like to find something like :contains() but to match an exact string. In other words, it shouldn\'t be a partial match. For example i

相关标签:
4条回答
  • 2020-11-30 07:52

    Simple way is "select element by exact match" using pseudo function.

    Solution Select element by exact match of its content

    0 讨论(0)
  • 2020-11-30 08:07

    You could try writing a Regular Expression and searching off of that: Regular expression field validation in jQuery

    0 讨论(0)
  • 2020-11-30 08:14

    You can use filter for this:

    $('#id').find('*').filter(function() {
        return $(this).text() === 'John';
    });
    

    Edit: Just as a performance note, if you happen to know more about the nature of what you're searching (e.g., that the nodes are immediate children of #id), it would be more efficient to use something like .children() instead of .find('*').


    Here's a jsfiddle of it, if you want to see it in action: http://jsfiddle.net/Akuyq/

    0 讨论(0)
  • 2020-11-30 08:16

    jmar777's answer helped me through this issue, but in order to avoid searching everything I started with the :contains selector and filtered its results

    var $results = $('#id p:contains("John")').filter(function() {
        return $(this).text() === 'John';
    });
    

    Heres an updated fiddle http://jsfiddle.net/Akuyq/34/

    0 讨论(0)
提交回复
热议问题