我写了这个小脚本来更改单击时的类颜色,它可以工作,但是我会第二次单击以恢复原色。
function changeColor1() {
document.getElementById("ip-custom").className = "red";
}
function init() {
document.getElementById("ip-custom").onclick = changeColor1;
}
window.onload = init();
.red {
color: #f00;
}
<button id="ip-custom">Example</button>
您可以red
像下面这样切换类。
function changeColor1() {
document.getElementById("ip-custom").classList.toggle('red');
}
完整片段
function changeColor1() {
document.getElementById("ip-custom").classList.toggle('red');
}
function init() {
document.getElementById("ip-custom").onclick = changeColor1;
}
window.onload = init();
.red {
color: #f00;
}
<button id="ip-custom">Example</button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句