最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Opposite of .find() - Stack Overflow

programmeradmin2浏览0评论

Is there an opposite of .find()?

Where $('.myclass').except('#myid');

Would grab all elements with .myclass except the element with #myid. I know I could do $('.myclass[id=myid]') in this example, but seems like it would be helpful in other cases.

Thanks

EDIT:

Thanks for the responses! Looks like I just missed seeing .not() and :not() in the documentation.

Is there an opposite of .find()?

Where $('.myclass').except('#myid');

Would grab all elements with .myclass except the element with #myid. I know I could do $('.myclass[id=myid]') in this example, but seems like it would be helpful in other cases.

Thanks

EDIT:

Thanks for the responses! Looks like I just missed seeing .not() and :not() in the documentation.

Share Improve this question edited Aug 5, 2015 at 2:10 vittore 17.6k6 gold badges45 silver badges84 bronze badges asked Apr 26, 2010 at 22:21 BenBen 8678 silver badges18 bronze badges 1
  • I think it would be awesome if they aliased .lose() to .not(). – Vivin Paliath Commented Apr 26, 2010 at 22:32
Add a comment  | 

8 Answers 8

Reset to default 10
$('.myclass').not('#myid');

http://api.jquery.com/not/

Try .not() – it removes any element matching the selector.

$('.myclass').not('#myid')

If you want a single string selector, then use this:

$('.myClass:not(#myid)')

This uses the :not() pseudo-class selector instead of the .not() filter function.

It seems counter-intuitive, but this single selector method may be slower at times, because getting elements via class (without filtering) is optimized, it filters each of those as the selector passes in this case.

The alternative, using .not() can be faster, depending on the number of elements matching .myclass, because finding an element by ID is a very fast operation, so excluding it from the set is rather quick.

Generic

$('selector:not(selector)').doStuff()

Specific

$('.myclass:not(#myid)').doStuff() 

I believe is the correct solution.

Visit http://api.jquery.com/not-selector/

For more use cases and examples.

I think you're looking for not()

Yes there are. You can use either jQuery's :not selector or .not() function. Sample code:

$('.something').not('.else')
$('.something:not(.not-me):not(.neither-me)')

As a side note, CSS3 has native :not pseudo-class.

You're all wrong. Not is not strictly the opposite of Find because Not only searches the current elements and not the descendents, while Find() will search through descendants to see if a certain criteria matches. The opposite of Find is instead .Not(:has(...)).

发布评论

评论列表(0)

  1. 暂无评论