猿问

怎么才能通过点按钮来回切换DIV的背景色呢?

<!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>


0_梦醒无痕_0
浏览 2097回答 2
2回答

Chuan_wei

<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>你试试,可以的!
随时随地看视频慕课网APP
我要回答