“所有但不是” jQuery选择器 [英] "All but not" jQuery selector

查看:124
本文介绍了“所有但不是” jQuery选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以选择(使用jQuery)HTML标记中的所有div,如下所示:

I can select (using jQuery) all the divs in a HTML markup as follows:

$('div')

但我想排除特定的 div (从上面的选择中说出 id =myid)。

But I want to exclude a particular div (say having id="myid") from the above selection.

如何使用Jquery函数执行此操作?

How can I do this using Jquery functions?

推荐答案

简单:

$('div').not('#myid');

使用 .not() 将从 $('div返回的集合中删除由给定的选择器匹配的元素)

您还可以使用 :not() selector:

You can also use the :not() selector:

$('div:not(#myid)');

两个选择器都做同样的事情,但 :not()更快,大概是因为jQuery的选择器引擎Sizzle可以将它优化为本机 .querySelectorAll() call。

Both selectors do the same thing, however :not() is faster, presumably because jQuery's selector engine Sizzle can optimise it into a native .querySelectorAll() call.

这篇关于“所有但不是” jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆