这很简单。
我想通过一个按钮更改框的颜色。
此“如果”功能有什么问题?
<body>
<div class="box" style="width: 50px; height: 50px; background: brown;"></div>
<button class="btn" value="brown">Color Change</button>
</body>
<script>
btn = document.querySelector('.btn');
box = document.querySelector('.box');
btn.addEventListener('click', () => {
if (box.style.backgroundColor = 'brown'){
box.style.backgroundColor = 'black';
} else{
box.style.backgroundColor = 'brown';
}
})
</script>
答案 0 :(得分:-1)
检查:
btn = document.querySelector('.btn');
box = document.querySelector('.box');
btn.addEventListener('click', () => {
if (box.style.backgroundColor == 'brown'){
box.style.backgroundColor = 'black';
} else{
box.style.backgroundColor = 'brown';
}
})
<div class="box" style="width: 50px; height: 50px; background: brown;"></div>
<button class="btn" value="brown">Color Change</button>