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">
有许多可能的原因导致此问题,但是最有可能的原因是您可能尚未安装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];
}
注意:未经测试。另外,您在数组末尾有一个空条目,因此我将其修复。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句