0

私は助けが必要です。以下のコードにリストされている 3 つの div の色を動的に変更する JavaScript を取得できません。以下に完全なコードを貼り付けました...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style>
.box{
width:100px;
height:100px;
}
</style>
</head>

<body>
<div class="box">
quote 1
</div>
<div class="box">
quote 2
</div>
<div class="box">
quote 3
</div>

<script type="text/javascript">
var bgcolorlist=new Array( "#ff33cc", "#cc33ff")

$(".box").css("background-color",bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)]);
</script>

</body>
</html>
4

1 に答える 1

0

でjQuery を使用して$(".box").css()いますが、ライブラリをリンクしていません。これを現在のスクリプトの前に追加します。

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

または、jQuery をまったく使用しないでください。

var boxes = document.querySelectorAll('.box');
for(var i=0; i<boxes.length; i++) {
    boxes[i].style.backgroundColor = bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];
}
于 2013-08-20T00:19:22.093 に答える