jQuery - Selecting all NON-Matches?
I 开发者_StackOverflowhave a list like this:
<div class="commentList>
<div name="version-1>stuff</div>
<div name="version-1>stuff</div>
<div name="version-1>stuff</div>
<div name="version-2>stuff</div>
<div name="version-3>stuff</div>
<div name="version-3>stuff</div>
</div>
I'd like a line of jquery that, given a version #, like 3, will update the.CommentList to only show div's with name="version-3" the others will be hidden?
Possible w/o IDs?
I'd use a class
here since name
isn't a valid attribute, then you can do this:
$(".commentList > div").hide(); //hide them all
$(".commentlist > .version-3").show(); //show what you want
If you must use name, the second line would look like this:
$(".commentlist > div[name='version-3']").show();
But..try and be valid with a class here.
would using the not selector be a bad idea?
$(".commentList").children().not("div[name='version-3']").hide();
although the idea of hiding them all first is not such a bad dea :)
$('.commentList > div[name!=version-'+num+']').hide();
but of course it wont toggle back any divs that may already be hidden. @Nick has a good answer.
精彩评论