2013-06-02 33 views
0

在這段代碼中使用樣式的方法。我從日誌中得到:不能在JS

TypeError: header_m.style is undefined

我該怎麼辦錯了嗎?

回答

4

getElementsByClassName返回倍數元素。

因此,您正在訪問它不正確。你想要什麼,在這種情況下,就是:

header[0].style.color = 'red'; 
    // ^[0] will get the first element with the class, 
    // which is in this case your is header element. [1] would get the second, etc. 

jsFiddle.

+0

這是偉大的!謝謝。 – edem

+0

@edem不用擔心男人!樂意效勞 – lifetimes