Questions tagged «nodevalue»

6
nodeValue vs innerHTML和textContent。如何选择?
我正在使用普通js来更改label元素的内部文本,但是我不确定应该基于什么理由使用innerHTML或nodeValue或textContent。我不需要创建新节点或更改HTML元素或其他任何内容,只需替换文本即可。这是代码示例: var myLabel = document.getElementById("#someLabel"); myLabel.innerHTML = "Some new label text!"; // this works myLabel.firstChild.nodeValue = "Some new label text!"; // this also works. myLabel.textContent = "Some new label text!"; // this also works. 我浏览了jQuery源代码,它只使用一次nodeValue,但是多次使用innerHTML和textContent。然后,我发现此jsperf测试表明firstChild.nodeValue明显更快。至少这就是我的解释。 如果firstChild.nodeValue快得多,那么有什么收获呢?是否得到广泛支持?还有其他问题吗?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.