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

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

问题描述

我有一个 div(假设 id 是容器"),其中包含许多元素,包括一个 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() 方法在选择所有子项后过滤掉选择:

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

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

如果您的问题是 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 选择器测试器上试用 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天全站免登陆