Is there a case insensitive jQuery :contains selector?

后端 未结 12 2133
悲哀的现实
悲哀的现实 2020-11-22 02:45

Is there a case insensitive version of the :contains jQuery selector or should I do the work manually by looping over all elements and comparing their .text() to my string?<

相关标签:
12条回答
  • To make it optionally case insensitive: http://bugs.jquery.com/ticket/278

    $.extend($.expr[':'], {
      'containsi': function(elem, i, match, array)
      {
        return (elem.textContent || elem.innerText || '').toLowerCase()
        .indexOf((match[3] || "").toLowerCase()) >= 0;
      }
    });
    

    then use :containsi instead of :contains

    0 讨论(0)
  • 2020-11-22 02:50

    I had a similar problem with the following not working...

    // This doesn't catch flac or Flac
    $('div.story span.Quality:not(:contains("FLAC"))').css("background-color", 'yellow');
    

    This works and without the need for an extension

    $('div.story span.Quality:not([data*="flac"])').css("background-color", 'yellow');
    

    This works too, but probably falls into the "manually looping" category....

    $('div.story span.Quality').contents().filter(function()
    {
      return !/flac/i.test(this.nodeValue);
    }).parent().css("background-color", 'yellow');
    
    0 讨论(0)
  • 2020-11-22 02:51

    In jQuery 1.8 you will need to use

    jQuery.expr[":"].icontains = jQuery.expr.createPseudo(function (arg) {                                                                                                                                                                
        return function (elem) {                                                            
            return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;        
        };                                                                                  
    });
    
    0 讨论(0)
  • 2020-11-22 02:52

    May be late.... but,

    I'd prefer to go this way..

    $.extend($.expr[":"], {
    "MyCaseInsensitiveContains": function(elem, i, match, array) {
    return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
    }
    });
    

    This way, you DO NOT tamper with jQuery's NATIVE '.contains'... You may need the default one later...if tampered with, you might find yourself back to stackOverFlow...

    0 讨论(0)
  • 2020-11-22 02:55

    A variation that seems to perform slightly faster and that also allows regular expressions is:

    jQuery.extend (
        jQuery.expr[':'].containsCI = function (a, i, m) {
            //-- faster than jQuery(a).text()
            var sText   = (a.textContent || a.innerText || "");     
            var zRegExp = new RegExp (m[3], 'i');
            return zRegExp.test (sText);
        }
    );
    



    Not only is this case-insensitive, but it allows powerful searches like:

    • $("p:containsCI('\\bup\\b')") (Matches "Up" or "up", but not "upper", "wakeup", etc.)
    • $("p:containsCI('(?:Red|Blue) state')") (Matches "red state" or "blue state", but not "up state", etc.)
    • $("p:containsCI('^\\s*Stocks?')") (Matches "stock" or "stocks", but only at the start of the paragraph (ignoring any leading whitespace).)
    0 讨论(0)
  • 2020-11-22 03:00
    jQuery.expr[':'].contains = function(a,i,m){
        return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
    };
    

    The update code works great in 1.3, but "contains" should be lower case on the first letter unlike the previous example.

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