<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
div{ width:100px; height:100px; background:red; margin-bottom:10px;}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
<script>
var aDiv=document.getElementsByTagName('div');
//点击每一个div
for(var i=0; i<aDiv.length; i++){
//每一个div上,自定义了一个color属性
aDiv[i].color=true;
//绑事件
aDiv[i].οnclick=function(){
//判断自己开关的状态
if(this.color==true){
this.style.background="blue";
this.color=false;
}else{
this.style.background="red";
this.color=true;
}
};
};
</script>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
div{ width:100px; height:100px; background:red; margin-bottom:10px;}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
<script>
var aDiv=document.getElementsByTagName('div');
//点击每一个div
for(var i=0; i<aDiv.length; i++){
//每一个div上,自定义了一个color属性
aDiv[i].color=true;
//绑事件
aDiv[i].οnclick=function(){
//判断自己开关的状态
if(this.color==true){
this.style.background="blue";
this.color=false;
}else{
this.style.background="red";
this.color=true;
}
};
};
</script>