admin管理员组

文章数量:1320661

I have the following line which works OK

$("#myDiv img:not(:eq(0))").hide();

I want to write a similar line but using "this". So:

$(this":not(:eq(0))").hide();

But that doesn't work... Any ideas where it's gone wrong?

I have the following line which works OK

$("#myDiv img:not(:eq(0))").hide();

I want to write a similar line but using "this". So:

$(this":not(:eq(0))").hide();

But that doesn't work... Any ideas where it's gone wrong?

Share Improve this question asked Mar 9, 2011 at 14:13 TomTom 13k50 gold badges153 silver badges247 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

try .not(selector) http://api.jquery./not/

The other answers are forgetting an important point - this is most likely in some event callback, and is likely a single element, so it is always the first element in the selection (:eq(0)).

Therefore each the following equivalent snippets will never hide anything:

$(this).not(':eq(0)').hide();
$(this).filter(':gt(0)').hide();
$(this).slice(1).hide();

I am only guessing the OP's intent here, but the code should most likely be:

if ($(this).index('#myDiv img') > 0) $(this).hide();

Something like this should work:

$(this).not(":eq(0)").hide();

It seems like you should be using the :gt() selector

Description: Select all elements at an index greater than index within the matched set.

try:

$(this).find(":gt(0)").hide();

or:

$(":gt(0)", this).hide();

isn't :not(:eq(0)) a clunky way of writing :gt(0)?

$($(this).selector + ":not(:eq(0))").hide();

本文标签: javascriptUsing not selector with (this)Stack Overflow