【发布时间】:2013-06-07 15:41:23
【问题描述】:
我一直在尝试切换按钮 onclick 的背景颜色属性,但颜色仅更改一次,并且不会来回切换。下面是代码。
function btnColor(btn, color) {
var property = document.getElementById(btn);
if (property.style.backgroundColor == "rgb(244,113,33)") {
property.style.backgroundColor=color;
}
else {
property.style.backgroundColor = "rgb(244,113,33)";
}
}
<input type="button" id="btnHousing" value="Housing" onclick="toggleLayer('transparent1');btnColor('btnHousing','rgb(255,242,0)');" />
【问题讨论】:
-
Console.log
property.style.backgroundColor就在if之前,您会看到实际值是多少。
标签: javascript html button background-color