JQuery:选择闭合标签之间的文本
类似的问题已经被问过,但还不够相似!
给定以下结构:
1这是第一点2这是第二点3
如何选择闭合标签之间的文本?即“这是第一点”。
内容是通过 JSON 调用反馈的,因此我无法对他们提供的结构做太多事情。
谢谢!
Similar questions have been asked but not quite similar enough!
Given this structure:
<p class="text">
<b>1</b>this is point one<b>2</b>this is point two<b>3</b>
</p>
How would I select the text between the closed tags? ie "this is point one".
The content is fed back via a JSON call so I can't do much with the structure they're giving me.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以将
#
替换为更容易拆分的内容,然后迭代结果示例 jsfiddle
jQuery:
can replace your
<b>#</b>
to something easier to split on then iterate over the resultsexample jsfiddle
jQuery:
如果您需要访问多个文本节点,您可以使用以下方法将其全部提取到一个数组中:
您现在可以使用
data[0].nodeValue
访问“this is point one”和“this is point”二”使用data[1].nodeValue
。工作小提琴: http://jsfiddle.net/jHhFS/
注意:附加条件 (
!this .nodeValue.match(/^\s*$/)
) 过滤掉仅包含空格的文本节点。If you need access to several of the text nodes, you can extract it all into an array using:
You can now access "this is point one" using
data[0].nodeValue
and "this is point two" usingdata[1].nodeValue
.Working fiddle: http://jsfiddle.net/jHhFS/
Note: The additional condition (
!this.nodeValue.match(/^\s*$/)
) filters out text nodes that contain only whitespaces.您可以使用 .contents() 获取该值。在你的情况下,这会起作用:
you can get to that value using .contents(). In your case this would work:
这是一个潜在的解决方案:
演示:http://jsfiddle.net/mrchief/3L8Ze/1/
Here's a potential solution:
Demo: http://jsfiddle.net/mrchief/3L8Ze/1/
你可以尝试 -
这取自 - 如何选择文本节点jQuery?。如链接问题中所述,该解决方案不适用于 IE7,但链接问题中有详细说明的解决方法。
工作演示 - http://jsfiddle.net/E53HU/1/
You could try -
This is taken from - How do I select text nodes with jQuery?. As stated in the linked question, the solution won't work with IE7, but there is a workaround detailed in the linked question.
Working demo - http://jsfiddle.net/E53HU/1/