如何操作html中的文本节点
在 html 中,文本节点仅包含纯文本。如果超链接位于其间,则它是一个单独的子节点,并且它成为元素节点。我想知道,我们能否以文本节点的方式操作锚标记。就像在某些 span 或 div 标签之间保留锚标签一样。我按照这个方法试过了,不行。有什么解决办法吗?
In html, a text node contains only pure text. If a hyperlink comes in between it is a separate childNode and it becomes element node. I want to know, that can we manipulate an anchor tag in such a way that it appears as a text node. Like by keeping anchor tag between some span or div tag. I tried it that way didn't work out. Is there any solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
锚标记的内容将在锚元素节点下形成一个文本节点。不可能有锚标记而没有锚元素节点。
结构如下所示: http://www.w3schools.com/htmldom/default.asp< /a>
例如,如果您有这样的代码:
没有办法让这段代码在没有锚元素的情况下出现在 DOM 中。添加 HTML 元素后,它将被添加到该页面的 DOM 树中。
The contents of the anchor tag will form a text node under the anchor element node. It is not possible to have an anchor tag and not have the anchor element node.
The structure is as shown here: http://www.w3schools.com/htmldom/default.asp
For example, if you have code like this:
There is no way to make this code appear in the DOM without the anchor element. Once you add an HTML element, it will be added the the DOM tree for that page.
假设
您有一个具有三个子节点的段落元素:一个文本节点、一个元素节点和一个文本节点。
听起来您想要段落节点中的所有文本。你可以参考
得到你想要的(我认为)。
现在,如果您想要获取锚节点的文本,作为文本节点,您可以通过识别存在一个文本节点作为锚节点的子节点来实现。
我在 http://jsfiddle.net/8Yqqz/1/ 创建了一个演示
基本思想是您找到锚节点,然后获取其第一个子节点。该子节点将是一个文本节点。小提琴的源代码应该清楚地说明这一点。
Given
you have a paragraph element with three children: a text node, an element node, and a text node.
It sounds like you want all the text within the paragraph node. You can refer to
to get what (I think) you want.
Now if you want to grab the text of the anchor node, as a text node you can do so by recognizing that there is a text node as a child of the anchor node.
I created a demo at http://jsfiddle.net/8Yqqz/1/
The basic idea is that you locate the anchor node, then get its first child. This child will be a text node. The source code of the fiddle should make this clear.
打印
link</a>
,您将获得带有link
的文本节点说起来,人们可以使用 DOM 客户端来做到这一点,比如
你真正想要什么?
Print
<a>link</a>
and you'll get text node with<a>link</a>
Technically speaking, one might do this with DOM clientside, like
What do you really want?