<!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>背景变色</title>
<style>
div{
width: 300px;
height: 300px;
background-color: blue;
margin: 30px;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<script>
var divs=document.getElementsByTagName("div")
for(var n=0;n<divs.length;n++){
divs[n].οnclick=function(){
this.style.backgroundColor="red"
}
}
</script>
</body>
</html>