DOM / Javascript:获取标签后的文字 [英] DOM/Javascript: get text after tag

查看:114
本文介绍了DOM / Javascript:获取标签后的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取html文档中的标签之后的文本

How do I get the text "there" that comes after a tag in an html document:

<p><a>hello</a>there</p>

我看到有一种方法可以用xpath:

I see that there is a way to do it with xpath:

从下一个标签获取文字

但我没有使用xpath,我希望不必为此开始。我意识到我可以得到所有的文本内的p标签,但我想得到只是在那里文本,以及知道它与p和一个标签的关系。它似乎不是任何人的孩子或兄弟姐妹。 (您可以假设我可以获得任何其他元素/节点,因此可以相对于这些元素。)每个DOM教程似乎都忽略了文本可以在标签之外发生的事实。

but I'm not using xpath and am hoping not to have to start just for this. I realize that I can get ALL the text inside the p tag, but I want to get just the "there" text, as well as know its relationship to the p and a tags. It doesn't seem to be anyone's child or sibling. (You can assume that I can get any of the other elements/nodes so it can be relative to those.) Every DOM tutorial seems to ignore the fact that text can occur outside tags.

谢谢。

推荐答案

这个

var p = document.getElementsByTagName("p")[0];
alert(p.childNodes[1].textContent)

这篇关于DOM / Javascript:获取标签后的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆