当我需要更改 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 ofElemRef.innerText
isElemRef.childNodes[0].nodeValue
.
关于html - 如何在 innerText 或 nodeValue 之间进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1760126/