Usage: not a selector with $ (this)

I have the following line that 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 it doesn’t work ... Any ideas where this went wrong?

+3
source share
5 answers

Other answers forget an important point - thismost likely, in some event callback, and this is probably a single element , therefore it is always the first element in the selection ( :eq(0)).

Therefore, each subsequent equivalent fragment will never hide anything :

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

I only guess the intent of OP here, but the code should probably be:

if ($(this).index('#myDiv img') > 0) $(this).hide();
+5
source
+6

- :

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

, :gt()

Description: Select all items with an index greater than the index in the matched set.

to try:

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

or

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

isn't a :not(:eq(0))clumsy way to record :gt(0)?

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

All Articles