更改 textNode 值

有什么办法可以改变网页浏览器中 DOM textNode 的值吗?

我特别想看看我是否可以 改变现有的节点,而不是 创造一个新的。

为了澄清,我需要用 Javascript 来做这件事。浏览器中的所有文本都存储在 # textNodes 中,这些节点是其他 HTML 节点的子节点,但不能有自己的子节点。

如下所述,可以通过设置这些 Object 的 NodeValue属性来更改内容。

79786 次浏览

I believe innerHTML is used for this... Then again, that isn't W3C approved... but it works...

node.innerHTML="new value";

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.