【发布时间】:2014-09-11 02:58:56
【问题描述】:
使用有什么不同:
// assuming using elements/tags 'span' creates an array and want to access its first node
1) var arrayAccess = document.getElementsByTagName('elementName')[0]; // also tried property items()
对比
// assuming I assign an id value to the first span element/tag
// specifically calling a node by using it's id value
2) var idAccess = document.getElementById('idValue');
那么如果我想更改文本节点....使用示例 1) 时它将不起作用,例如:
arrayAccess.firstChild.nodeValue = 'some text';
或
arrayAccess.innerText/innerHTML/textContent = 'some text';
如果我通过它的 id 值“访问”节点,那么它似乎工作正常......
为什么在使用数组时它不起作用?我是 javascript 新手,我正在阅读的书没有提供答案。
【问题讨论】:
-
当一切都失败时,总会有规范:getElementById,getElementsByTagName。请注意,getElementsByTagName 返回一个 NodeList,不是一个数组。
标签: javascript arrays text