Is there a case insensitive jQuery :contains selector?

后端 未结 12 2351
悲哀的现实
悲哀的现实 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条回答
  •  暖寄归人
    2020-11-22 03:13

    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());
    };
    

提交回复
热议问题