我有这个代码在单击时更改我的按钮内部HTML。我还想在单击按钮时更改CSS背景颜色,但我无法使其工作。
<button id="showTop" class="menubtn" >Show Menu</button>
var menuTop = document.getElementById('cbp-spmenu-s3'),
showTop = document.getElementById('showTop'),
body = document.body;
showTop.onclick = function() {
if (showTop.innerHTML == 'Show Menu') {
showTop.innerHTML = 'Hide Menu';
} else {
showTop.innerHTML = 'Show Menu';
};
classie.toggle(this, 'active1');
classie.toggle(menuTop, 'cbp-spmenu-open');
disableOther('showTop');
};
function disableOther(button) {
if (button !== 'showTop') {
classie.toggle(showTop, 'disabled');
}
}
答案 0 :(得分:1)
你的意思是这样吗?
if (showTop.innerHTML == 'Show Menu') {
showTop.style.backgroundColor = "blue";
showTop.innerHTML = 'Hide Menu';
} else {
showTop.innerHTML = 'Show Menu';
showTop.style.backgroundColor = "";
};