开发者

jquery filter out specific element

I'm implementing accordion-style behavior, where only one element can be displayed at one point. I thought it'd be simplistic to implement a show(e) function that takes an element, e, to be displayed and hides everything BUT e. It saves me the hassle of keeping track of which element is being displayed as well. I could just attach show(e) as a callback to each element of the accordion.

To do so, I was thinking the only way is to use .each() to iterate through all the elements ad compare each one to e, hiding it if it isn't equal to e.

However, I recall there being a jQuery .filter function (http://api.jquery.com/filter/) but it only matches with the element, and not the opposite. (i.e. if I call the function and pass it e it'll only match e and not everything BUT e.)

Is there any way to do so, or are there any recommendations for creating an accordion in general? Thanks in advance!开发者_运维技巧


Well then you should consider using .not() function.

var excludeTheseDOMElements = $('#something');
$('match_something').not(excludeTheseDOMElements).each(function(){
     //do stuff here
});

UPDATE:

You can also use the :not() pseudo selector -

$('match_something:not(#something)').each(function(){
    //do stuff here
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜