jQuery: use filter(), but work with both results

后端 未结 6 1838
孤城傲影
孤城傲影 2021-01-01 23:16

In jQuery, filter() reduces your result to those elements that fulfill a certain condition.

This splits the list in two parts. Working with the \"good

6条回答
  •  不知归路
    2021-01-01 23:36

    The method recommended by Kobi in plugin form:

    $.fn.invert = function() {
      return this.end().not(this);
    };
    
    $('.foo').filter(':visible').hide().invert().show();
    

    Note that invert() will not add a new element to the jQuery stack but replace the last one:

    $('.foo').filter(':visible').invert().end(); // this will yield $('.foo'), not $('.foo:visible')
    

    Edit: changed prevObject to end() at Tomalak's suggestion.

提交回复
热议问题