开发者

Jquery - .fadeOut all div's except those with these any of these id's?

开发者 https://www.devze.com 2022-12-25 21:06 出处:网络
How can I fade out all div\'s besides those with any of these three id\'s? #dontFadeOne #dontFadeTwo #dontFadeThree

How can I fade out all div's besides those with any of these three id's?

#dontFadeOne
#dontFadeTwo
#dontFadeThree

I'm thinking something like this:

$("div").notAnyOf("dontFadeOne","dontFadeTwo","dontFadeThree").fad开发者_运维问答eOut();

But I can't find a 'not any of' function.


Have a look at the .not() function:

http://api.jquery.com/not/

Since the official doc explains this quite clearly, here it is from the API website:

Removing Specific Elements

The second version of the .not() method allows us to remove elements from the matched set, assuming we have found those elements previously by some other means. For example, suppose our list had an id applied to one of its items:

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li id="notli">list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
</ul>

We can fetch the third list item using the native JavaScript getElementById() function, then remove it from a jQuery object:

$('li').not(document.getElementById('notli'))
  .css('background-color', 'red');

This statement changes the color of items 1, 2, 4, and 5. We could have accomplished the same thing with a simpler jQuery expression, but this technique can be useful when, for example, other libraries provide references to plain DOM nodes.

As of jQuery 1.4, the .not() method can take a function as its argument in the same way that .filter() does. Elements for which the function returns true are excluded from the filtered set; all other elements are included.


You can use not() or :not for this:

$("div").not("#dontFadeOne, #dontFadeTwo, #dontFadeThree").fadeOut();

or:

$("div").not("#dontFadeOne).not("#dontFadeTwo").not("#dontFadeThree").fadeOut();

or

$("div:not(#dontFadeOne):not(#dontFadeTwo):not(#dontFadeThree)").fadeOut();
0

精彩评论

暂无评论...
验证码 换一张
取 消