更改textNode值 [英] Change textNode value

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

问题描述



我是否有任何方法可以更改Web浏览器中DOM文本节点的值?我特别想看看我是否可以更改现有的节点,而不是创建一个新的节点。



要澄清,我需要使用Javascript。浏览器中的所有文本都存储在#textNodes中,它们是其他HTML节点的子节点,但不能拥有自己的子节点。



正如下面回答的,内容可以更改通过设置这些对象的 nodeValue 属性。

解决方案

如果你有一个特定的节点#text),并希望更改其值,您可以使用 nodeValue 属性:

  node.nodeValue =new value; 

注意:



innerText textContent)将返回/设置当前节点和所有后代节点文本,因此可能不是您想要/期望的行为。


Is there any way to change the value of a DOM textNode in web browser?

I specifically want to see if I can change the existing node, rather than creating a new one.

To clarify, I need to do this with Javascript. All text in the browser is stored in #textNodes which are children of other HTML nodes, but cannot have child nodes of their own.

As answered below, content can be changed by setting the nodeValue property of these Objects.

解决方案

If you have a specific node (of type #text) and want to change its value you can use the nodeValue property:

node.nodeValue="new value";

Note:

innerText (and possibly textContent) will return/set both the current node and all descendent nodes text, and so may not be the behaviour you want/expect.

这篇关于更改textNode值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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