【发布时间】:2015-05-29 16:22:56
【问题描述】:
我正在尝试获取文本和图像的随机生成器,这应该非常简单,但我终其一生都无法弄清楚为什么它不起作用。非常感谢任何帮助!
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] + ')'});
});
提前致谢!
【问题讨论】:
-
您是否在控制台中看到任何错误?
x的值是多少? -
Uncaught TypeError: undefined is not a function on script.js line 7 which is the var x=images[math.floor(math.random() *images.length)];
-
您尝试过 Blazemonger 的建议吗?你应该有
var x = Math.Floor(Math.random() * images.length),而不是x=images[math.floor(math.random() *images.length)]; -
是的,我在下面回复了他们,但仍然无法正常工作:(
-
请尝试使用
console.log(x)并告诉我结果。
标签: javascript jquery html css arrays