如果有同一组元素,我们想要某一个元素实现某种样式,需要用到循环的排他思想算法:
1、所有元素全部清除样式(干掉其他人)
2、给当前的元素设置样式(留下我自己)
3、注意不能颠倒顺序,首先干掉其他人,在设置我自己
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
</style>
</head>
<body>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<script>
var btns = document.getElementsByTagName('button');
for(var i=0;i<btns.length;i++){
btns[i].onclick = function (){
for(var i=0;i<btns.length;i++){
btns[i].style.backgroundColor = '';
}
this.style.backgroundColor = 'blue';
}
}
</script>
</body>
</html>