我似乎无法访问某些元素的内容,我认为这一定是因为我使用的语法。我正在尝试编写和覆盖 getElementsByClassName 列表的成员。我知道它是一个 nodeList 而不是一个数组,但我仍然无法弄清楚。这是我已经/尝试过的:
HTML:
<span class="myclass">Text</span>
<span class="myclass">Text2</span>
JS:
var spanarray = document.getElementsByClassName("myclass");
//I've tried all of the following (for the record, I thought either 2 or 5 would work)
spanarray[0] = "Replacement text";
spanarray[0].innerHTML = "Replacement text";
spanarray.item(0) = "Replacement text";
spanarray.item(0).innerHTML = "Replacement text";
spanarray.item(0).nodeValue = "Replacement text";
如果有人能指出我正确的方向,我将不胜感激。谢谢。