Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript在div上动态更改颜色_Javascript_Css_Background Color - Fatal编程技术网

使用javascript在div上动态更改颜色

使用javascript在div上动态更改颜色,javascript,css,background-color,Javascript,Css,Background Color,我需要帮助。我无法让javascript动态更改下面代码中列出的三个div的颜色。我已经在下面粘贴了完整的代码 <!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"><he

我需要帮助。我无法让javascript动态更改下面代码中列出的三个div的颜色。我已经在下面粘贴了完整的代码

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

无标题文件
.盒子{
宽度:100px;
高度:100px;
}
引文1
引文2
引文3
var bgcolorlist=新数组(“#ff33cc”,“#cc33ff”)
$(“.box”).css(“背景色”,bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];

您正在
$(“.box”).css()上使用jQuery,但尚未链接库!在当前脚本之前添加以下内容:

<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)];
}
var-box=document.querySelectorAll('.box');
对于(var i=0;i