:contains for multiple words

后端 未结 4 1291
小蘑菇
小蘑菇 2021-01-13 19:49

I am using the following jQuery

var etag = \'kate\'
if (etag.length > 0) {
    $(\'div\').each(function () {
        $(this).find(\'ul:not(:contains(\' +         


        
4条回答
  •  独厮守ぢ
    2021-01-13 20:25

    This filter checks if any of the words in the given string match the text of the element.

    jQuery.expr[':'].containsAny = function(element, index, match) {
        var words = match[3].split(/\s+/);
        var text = $(element).text();
        var results = $.map(words, function(word) {
            return text === word;
        });
        return $.inArray(true, results) !== -1;
    };
    

    Show and hide as:

    $('ul').hide();
    $('li:containsAny(john kate)').parents('ul').show();
    

    See an example here.

提交回复
热议问题