如何处理html中的文本节点 [英] How to manipulate text nodes in html

查看:177
本文介绍了如何处理html中的文本节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML中,文本节点只包含纯文本。如果一个超链接在它之间是一个单独的childNode并且它成为元素节点。我想知道,我们可以这样操纵一个锚标签,使它看起来像一个文本节点。像在一些span或div标签之间保持锚标签。我试过这样没有办法。有没有解决方案?

解决方案

给定

 code>< p id =message> Hello< a href =#>来自此< / a>页面< / p为H. 

您有一个带有三个孩子的段落元素:文本节点,元素节点和文本节点



这听起来像您想要段落节点中的所有文本。你可以参考

  document.getElementById(message)。innerHTML 
/ pre>

得到你想要的(我想)。



现在,如果你想抓住文本作为文本节点的 ,您可以通过识别有一个文本节点作为锚节点的子节点来实现。



我在 http://jsfiddle.net/8Yqqz/1/ 上创建了一个演示。



基本思想是找到锚节点,然后获取其第一个孩子。这个孩子将是一个文本节点。小提琴的源代码应该清楚。


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?

解决方案

Given

<p id="message">Hello <a href="#">from this</a> page</p>

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

document.getElementById("message").innerHTML

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.

这篇关于如何处理html中的文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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