Javascript,通过IF在一个按钮中执行两个操作
这很简单。 我想用一个按钮改变一个盒子的颜色。 这个“如果”函数有什么问题Javascript,通过IF在一个按钮中执行两个操作,javascript,button,addeventlistener,Javascript,Button,Addeventlistener,这很简单。 我想用一个按钮改变一个盒子的颜色。 这个“如果”函数有什么问题 <body> <div class="box" style="width: 50px; height: 50px; background: brown;"></div> <button class="btn" value="brown">Color Change</butt
<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>
选中此项:
btn=document.querySelector'.btn';
box=document.querySelector'.box';
btn.addEventListener'click',=>{
如果box.style.backgroundColor=='brown'{
box.style.backgroundColor='黑色';
}否则{
box.style.backgroundColor='brown';
}
}
在if子句中,颜色更改=赋值,而不是比较===。