<!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>DIV变色练习</title> <style type="text/css"> div { margin:0 auto; width:600px; height:200px; background-color:yellow; } p { text-align:center; } </style></head><body> <div id="div-one" ></div> <p> <button type="button" onclick="objDiv()">点我</button> </p> <script> function objDiv(){ a=document.getElementById("div-one"); a.style.background="green" } </script></body></html>
2 回答
已采纳
Chuan_wei
TA贡献28条经验 获得超15个赞
<script type="text/javascript">
var colorTag = 1;
var colors = ["green", "yellow"];
function objDiv() {
colorTag = 1 - colorTag;
document.getElementById("div-one").style.backgroundColor = colors[colorTag];
}
</script>
你试试,可以的!
添加回答
举报
0/150
提交
取消