问题遇到的现象和发生背景
想添加一个功能 将已变背景色的按钮被点击时能取消背景色
问题相关代码,请勿粘贴截图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.bg1{background: aquamarine;}
.bg2{background: red;}
</style>
</head>
<body>
<button class="bg1">第1个</button><button>第2个</button><button>第3个</button><button>第4个</button><button>第5个</button>
<script>
let btn = document.querySelectorAll('button')
for(let i = 0;i<btn.length;i++){
btn[i].addEventListener('click',function(){
document.querySelector('.bg1').classList.remove('bg1')
btn[i].classList.add('bg1')
})
}
</script>
</body>
</html>