gpt4 book ai didi

jquery - 如何使用 jQuery 选择除 select 元素之外的所有子元素

转载 作者:行者123 更新时间:2023-12-03 21:27:02 25 4
gpt4 key购买 nike

我有一个 div(假设 id 是“容器”),其中包含许多元素,包括一个 select 元素。我想选择 div 中除 select 之外的所有内容。我尝试过的事情:

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

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

也尝试过不使用通配符后代选择器,所以就像上面所有的一样,但是

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

最佳答案

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

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

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

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

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

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

或仅选择直接子项:

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

您可以在 the interactive jQuery selector tester 尝试 jQuery 选择器快速反馈;尝试例如 div :not(ol)div > :not(ol) 来感受一下直接子项 (> ) 选择器使。

关于jquery - 如何使用 jQuery 选择除 select 元素之外的所有子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1125700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com