:contains for multiple words

后端 未结 4 1273
小蘑菇
小蘑菇 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:29

    A slightly different approach - try this:

        var etag='kate test blah';
    
        var tags = etag.split(' ');
        $('div ul').each(function () {
          var list = $(this);
          list.hide();
          list.children('li').each(function() {
            var item = $(this);
            if ($.inArray(item.html(), tags) >= 0) list.show();
          });
        });
    

    Written in the browser, so sorry if there are any bugs!

    -- edit --

    Actually, I reread the question, and that code won't work. You need to define the behaviour more first - does the list have to contain all of the tags, or just one of the tags to be shown?

    -- edit 2 --

    Code updated. Kind of inefficient, but it should do the job.

提交回复
热议问题