2个回答
展开全部
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button>A</button>
<button>B</button>
<button>C</button>
<script>
var btn = document.getElementsByTagName("button");
for(var i=0;i<btn.length;i++){
(function(index){
btn[index].onclick = function(){
btn[index].style.backgroundColor = "red";
}
}(i))
}
</script>
</body>
</html>
追问
可以变回去吗。
追答
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button>A</button>
<button>B</button>
<button>C</button>
<script>
var btn = document.getElementsByTagName("button");
for(var i=0;i<btn.length;i++){
(function(index){
btn[index].onclick = function(){
if(btn[index].style.backgroundColor === "red"){
btn[index].style.backgroundColor = "";
}else{
btn[index].style.backgroundColor = "red";
}
}
}(i))
}
</script>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询