我試圖做一個菜單,但是當我試圖在Chrome中執行代碼時,我得到了一個錯誤。JavaScript getElementsByClassName錯誤
所以對於調試建議我做了一個按鈕:
<input type="button" style="position: fixed; left:0; bottom:0; width:50px; height:25px;" value="test" onclick="login('logf')" />
這是錯誤的功能是:
var loginshow = false;
function login(c)
{
e = document.getElementsByClassName(c);
if(loginshow)
{
e.style.height = "0";
}
else
{
e.style.height = "110px";
}
loginshow=!loginshow;
}
從谷歌Chrome錯誤:
bar.js:13 Uncaught TypeError: Cannot set property 'height' of undefined
at login (bar.js:13)
at HTMLInputElement.onclick ((index):32)
'getElementsByClassName'返回數組 – Weedoze
你使用哪種工具,你的下發展應用?如果你希望你可以鍵入調試器;就在您的javascript代碼之前,當您瀏覽您的應用程序時,您可以在Google Chrome瀏覽器中按F12鍵打開源代碼視圖並調試您的代碼。代碼調試器;會受到打擊。你可以看到,可能存在與類名等錯誤.. –
@Weedoze是正確的,'e'是一個數組,所以你需要遍歷元素陣列上,或者如果你只知道有一個,稱其爲'E [0] .style.height'的 – Mikkel