点击按钮,按钮背景颜色变为粉色,且只有一个按钮会变色,其余按钮复原。
<!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>
.nav {
display: inline-block;
width: 60px;
height: 30px;
border: 1px solid black;
border-radius: 15px;
text-align: center;
line-height: 30px;
}
</style>
</head>
<body>
<div class="nav">手机</div>
<div class="nav">电脑</div>
<div class="nav">安卓</div>
<div class="nav">电视</div>
<script>
var nav_list = document.querySelectorAll('.nav');
console.dir(nav_list);
for(var i = 0 ;i<nav_list.length;i++){
nav_list[i].onclick = function() {
for(var i = 0 ;i<nav_list.length;i++){
nav_list[i].style.backgroundColor = '';
}
this.style.backgroundColor = 'pink';
}
}
</script>
</body>
</html>