<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>排他思想</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
</head>
<body>
<button>按钮一</button>
<button>按钮二</button>
<button>按钮三</button>
<button>按钮四</button>
<button>按钮五</button>
<script>
var btn = document.getElementsByTagName('button');
for(var i = 0; i < btn.length; i++)
{
//btn得到的是伪数组
btn[i].onclick = function(){
//判断是否有已经选中的按钮
for(var i = 0; i<btn.length; i++){
btn[i].style.backgroundColor='';
}
this.style.backgroundColor= 'pink';
}
}
</script>
</body>
</html>
实现画面: