如何使用jQuery选择除select元素之外的所有子项 [英] How do I use jQuery to select all children except a select element

查看:125
本文介绍了如何使用jQuery选择除select元素之外的所有子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div(让我们说id是container),其中包含许多元素,包括一个select元素。我想选择除了选择之外的所有div。
我尝试过的东西:

I have a div (let's say the id is "container") with many elements in it, including a select element. I'd like to select all everything in the div except the select. Things I've tried:

$("#container *:not(select)")
$("#container *:not(#selectorid)")

//put a div around the select and...
$("#container *:not(#selectorcontainer)")
$("#container *:not(#selectorcontainer *)")
$("#container *:not(#selectorcontainer, #selectorcontainer *)")

也尝试没有通配符后代选择器,所以就像上面所有,但是

Also tried without wildcard descendant selector, so just like all the above, but

$("#container:not(#selectorid)")


推荐答案

在这种情况下,您可以简单地省略通配符,因为它是可选的,但保留空格:

You can simply omit the wildcard as it is optional in this case, but keep the space:

$('#container :not(select)');

或者,使用.not()方法在选择所有子项后过滤掉选择: p>

Alternatively, use the .not() method to filter out the select after selecting all children:

$('#container').children().not('select');

如果您的问题是的孩子选择仍然包含在内,您可以使用.not()方法显式过滤掉它们:

If your problem is that children of select are still included, you could explicitly filter those out with the .not() method:

$('#container').children().not('select, option, optgroup');

或仅选择直接的孩子:

$('#container > :not(select)');

您可以尝试在交互式jQuery选择器测试器,以获得快速反馈;尝试例如 div:not(ol) div> :not(ol)以了解直接孩子(> )选择器的区别。

You can try out jQuery selectors at the interactive jQuery selector tester for quick feedback; try for example div :not(ol) or div > :not(ol) to get a feel for what difference the direct child (>) selector makes.

这篇关于如何使用jQuery选择除select元素之外的所有子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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