如何使用JQuery选择没有特定子元素的元素 [英] How to select elements which do not have a specific child element with JQuery
本文介绍了如何使用JQuery选择没有特定子元素的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个JQuery选择器来选择所有不没有某些子元素作为直接子元素的元素?例如:
Is there a JQuery selector to select all elements which do not have certain child element as a direct child? For example:
<p>
text in paragraph
</p>
<p>
<div>text in div</div>
</p>
我想只选择<p>
,如第一个(没有<div>
子代). 这可能吗?
I want to select only <p>
s like the first one (without a <div>
child). Is this possible?
更多信息:实际上,我正在尝试将<div>
插入所有没有这样的表达式的<p>
中:
Further information: Actually I'm trying to insert a <div>
into all those <p>
that do not have one by an expression like this:
$('p').wrapInner('<div />')
但这会在第二个<p>
上添加一个额外的<div>
.
But this would add an additional <div>
to the second <p>
.
推荐答案
您可以尝试:
$("p:not(:has(>div))")
这篇关于如何使用JQuery选择没有特定子元素的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文