我試圖用class
改變一個Id
的內容,我不知道爲什麼這是不工作...getElementByClassName返回'undefined',但它適用於Id?
<h1 id="header">header</h1>
<h4 id="sub">sub header</h4>
<span class="content">Please work!</span>
此函數返回「未定義」,而我的文本編輯器似乎沒有認識到getElementsByClassName
- 它正常工作與getElementById
var test = document.getElementsByClassName("content").innerHTML;
document.getElementById("header").innerHTML = test;
當使用'document.getElementsByClassName(「content」)'它會返回一個對象集合(很像數組)!你必須這樣做:'document.getElementsByClassName(「content」)[index]' – www139
@ www139 - 它返回一個[* HTMLCollection *](http://www.w3.org/TR/2011/WD-html5- author-20110705/common-dom-interfaces.html#htmlcollection-0),而不是數組。 – RobG
@RobG謝謝你指出:) – www139