jQuery method to select a subset and also its inverse?

前端 未结 2 391
说谎
说谎 2021-01-20 11:33

Given some jQuery object set, and also a subset of that set, is there a method that will return the inverse subset?

If the answer is \"No

2条回答
  •  执念已碎
    2021-01-20 12:05

    Here is a little extension to jquery about it that i just made..

    (function($) {
      $.fn.reverse = function () 
      {
          var orig = this.prevObject || jQuery(null);
          return orig.not(this);
      }
    })(jQuery);
    

    It will maintain the chaining, so you can still do .end() after it and return to the previous filtering.

    You can call it like

    $('li').css('background-color','blue')
      .filter('.subset')
        .css('color','black')
      .reverse()  // <-- White Whale?!?
        .css('color','white');
    

    Demo with your example: http://www.jsfiddle.net/gaby/THLkn/

提交回复
热议问题