jquery 选择器 - 选择不在另一个嵌套标签内的值
是否可以选择不在另一个嵌套标签内的标签的值?
例如,在下面的代码中,我想从 $('#example') 获取“我想选择的文本”。
<td id="example">
<a> Text I don't want to select</a>
<span> Other text I don't want to select</span>
Text I want to select
<anyOtherTag> Other text I don't want to select</anyOtherTag>
</td>
谢谢。
Is it possible to select the value of a tag which is not inside another nested tag?
For example in the following code I want to get ' Text I want to select' from $('#example').
<td id="example">
<a> Text I don't want to select</a>
<span> Other text I don't want to select</span>
Text I want to select
<anyOtherTag> Other text I don't want to select</anyOtherTag>
</td>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
.contents()
和.filter()
向下到文本节点类型 (nodeType == 3
) ,就像这样:您可以在这里尝试。由于
.text()
获取所有文本节点,包括其他空白,您可能需要$.trim()
(因为 IE<9 没有String.trim()
) 结果就像我上面的那样。You can use
.contents()
and.filter()
down to text node types (nodeType == 3
), like this:You can try it out here. since
.text()
gets all text nodes, including the other whitespace, you probably want to$.trim()
(since IE<9 doesn't haveString.trim()
) the result like I have above.