我正在试图获得一个随机的文本和图像生成器,这应该是超级简单但我不能为我的生活弄清楚为什么它没有成功。非常感谢任何帮助!
HTML 滚动演示#1
<body>
<div id="container">
<p>Where would you live and what would you drive?</p>
<p id="text"></p>
</div>
</body>
CSS
#container {
width: 960px;
margin-left: auto;
margin-right: auto;
border: gray solid;
padding: auto;
background-image: url(../images/house.jpg) no-repeat;
}
#text {
font-size: 40px;
text-align: left;
}
p {
width: 100px;
color: black;
font-family: arial;
font-size: 18px;
text-align: left;
}
JQUERY
$(document).ready(function(){
var car = ["limo", "golf cart", "pig"];
var images = ["house.jpg", "dumpster.jpg", "mansion.jpg"];
var x = images[Math.Floor(Math.random() * images.length)];
document.getElementById("text").innerHTML=car[x];
$('html').css({'background-image':'url(images/' + images [x] + ')'});
});
提前致谢!
答案 0 :(得分:4)
您似乎期望x
是一个数字,而不是图像:
var x = Math.floor(Math.random() * images.length); // not Floor