var images = ["https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Playing_card_heart_5.svg/200px-Playing_card_heart_5.svg.png", "http://www.pandasecurity.com/mediacenter/src/uploads/2014/11/short-link.jpg", ];
function myFunction() {
var x = Math.floor((Math.random() * images.length); $('#afbeelding').attr('src', images[x]);
}HTML:
<button onclick="myFunction()">Try it</button>
<img id="afbeelding">发布于 2016-05-12 14:02:13
造成此问题的原因有很多,但最有可能的原因是您可能没有安装JQuery。实际上,您甚至不需要JQuery:
var images = ["https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Playing_card_heart_5.svg/200px-Playing_card_heart_5.svg.png", "http://www.pandasecurity.com/mediacenter/src/uploads/2014/11/short-link.jpg"];
function myFunction() {
var x = Math.floor((Math.random() * images.length));
document.getElementById('afbeelding').src = images[x];
}注:未经测试。另外,在数组末尾有一个空条目,所以我修正了它。
发布于 2016-05-12 14:03:32
看看我用你的代码做的这个柱塞。顺便说一句")“在images.length https://plnkr.co/edit/mdyXsf5FoSPHOQGqCoO2?p=preview之后不见了
var x = Math.floor((Math.random() * images.length)); $('#afbeelding').attr('src', images[x]);如果可以的话告诉我
https://stackoverflow.com/questions/37189071
复制相似问题