开发者

Using :not selector with $(this)

开发者 https://www.devze.com 2023-02-15 15:12 出处:网络
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:

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"开发者_StackOverflow社区:not(:eq(0))").hide();

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


try .not(selector) http://api.jquery.com/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();
0

精彩评论

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