html - 如何在 innerText 或 nodeValue 之间进行选择?

标签 html innertext nodevalue

当我需要更改 span 元素中的文本时,我应该使用哪个,有什么区别:

var spnDetailDisplay=document.getElementById('spnDetailDisplay');
spnDetailDisplay.innerText=sDetail;

 var spnDetailDisplay=document.getElementById('spnDetailDisplay');
 spnDetailDisplay.childNodes[0].nodeValue=sDetail;

最佳答案

对于有文本内容的元素,它们是一样的。参见 this MDC article有关 nodeValue 的信息。

来自 this article :

If the element has no sub-elements, just text, then it (normally) has one child node, accessed as ElemRef.childNodes[0]. In such precise case, the W3C web standards equivalent of ElemRef.innerText is ElemRef.childNodes[0].nodeValue.

关于html - 如何在 innerText 或 nodeValue 之间进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1760126/

相关文章:

javascript - 如何发送推送通知 chrome android 移动设备 pwa

javascript - 如何检查元素的直接文本内容?

java - 如何使用 webdriver 通过内部文本查找元素

javascript - 使用 Javascript 获取 XML 节点值

html - 带孔的半透明覆盖层/掩模

Javascript:返回一个绑定(bind)输入,该输入是先前输入的结果

javascript - 如何删除输入字段的边框底部?

html - 我该如何提取这段文字

PHP DOM : Get Nodevalue without descendant nodes

javascript - ChildNodes/NodeValue 混淆