【问题标题】:Simple JQuery Array简单的 JQuery 数组
【发布时间】: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


【解决方案1】:

您似乎希望 x 是一个数字,而不是图像:

var x = Math.floor(Math.random() * images.length); // not Floor

【讨论】:

  • 感谢您的回复。我确实改变了,但它的行为仍然不准确。 :(
  • 天哪,非常感谢。我不敢相信这是问题所在。你是最棒的!!
猜你喜欢
  • 2013-10-07
  • 1970-01-01
  • 2015-09-12
  • 2015-09-22
  • 1970-01-01
  • 2023-03-25
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多