Is there a case insensitive jQuery :contains selec

2020-01-22 11:17发布

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条回答
仙女界的扛把子
2楼-- · 2020-01-22 12:12

A faster version using regular expressions.

$.expr[':'].icontains = function(el, i, m) { // checks for substring (case insensitive)
    var search = m[3];
    if (!search) return false;

    var pattern = new RegExp(search, 'i');
    return pattern.test($(el).text());
};
查看更多
甜甜的少女心
3楼-- · 2020-01-22 12:13
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.

查看更多
你好瞎i
4楼-- · 2020-01-22 12:14

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

查看更多
Emotional °昔
5楼-- · 2020-01-22 12:14

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...

查看更多
该账号已被封号
6楼-- · 2020-01-22 12:16

Refer below to use ":contains" to find text ignoring its case sensitivity from an HTML code,

 $.expr[":"].contains = $.expr.createPseudo(function(arg) {
            return function( elem ) {
                return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
            };
        });
        $("#searchTextBox").keypress(function() {
          if($("#searchTextBox").val().length > 0){
            $(".rows").css("display","none");
            var userSerarchField = $("#searchTextBox").val();
            $(".rows:contains('"+ userSerarchField +"')").css("display","block");
          } else {
            $(".rows").css("display","block");
          }              
        });

You can also use this link to find case ignoring code based on your jquery version, Make jQuery :contains Case-Insensitive

查看更多
太酷不给撩
7楼-- · 2020-01-22 12:16

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');
查看更多
登录 后发表回答