-2
我對Javascript很新,所以請隨時指出我做錯的任何事情。我試圖每次點擊按鈕時更改div的顏色。這是我的代碼到目前爲止:如何使用Javascript更改一個按鈕的div的顏色?
function setBgColour(){
if (.backgroundColor == '#ff0000'){
document.getElementsByClassName("light")[0].style.backgroundColor = '#ffff00';
} else if (.backgroundColor == '#ffff00'){
document.getElementsByClassName("light")[0].style.backgroundColor = '#00ff00'
} else if (.backgroundColor == '#00ff00'){
document.getElementsByClassName("light")[0].style.backgroundColor = '#ff0000'
}
}
window.onload = function(){
document.getElementById('next').addEventListener('click', setBgColour);
};
會是更好的存儲陣列中的顏色?並通過它們循環 – Dale
刪除if中的點。 –
除非有理由不這樣做。我建議你開始使用jQuery。它使JavaScript變得更容易。 – Jay