JQuery for selecting a subset as well as its inverse?

Given some set of jQuery objects, as well as a subset of this set, is there a method that will return a substring inverse ?

If the answer is “ No, ” is there a way to avoid the second choice in order to get the opposite subset?

Explanatory example:

<ul>
  <li class="subset"></li>
  <li class="subset"></li>
  <li class="inverse"></li>
  <li class="inverse"></li>
</ul>

First I want to do something with everything <li>, and then something only .subsetand finally something else only .inverse:

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

I know this can be easily done with help .end().filter('.inverse'), but suppose the selector was actually big and nasty , and running it twice would be a big hit . What then?

API, jQuery , , - ( .andSelf() , ...).

+3
2

, - , ... . .not(), :

var all = $('li').css('background-color','blue');
var sub = all.filter('.subset').css('color','black');
all.not(sub).css('color','white');

... , .end().filter(). , , .not() , ( ).


( ) :

$('li').css('background-color','blue').css('color','white')
       .filter('.subset').css('color','black');
+5

jquery, .

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

, .end() .

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

: http://www.jsfiddle.net/gaby/THLkn/

+1

Source: https://habr.com/ru/post/1781498/


All Articles