更新时间:2023-11-10 22:38:34
如何:
HTML:
< img id =image/>
JS:
var description = [
http://static.ddmcdn.com/gif/lightning-gallery-17.jpg,
http://static.ddmcdn.com/ gif / lightning-gallery-18.jpg,
http://static.ddmcdn.com/gif/lightning-gallery-19.jpg,
http://static.ddmcdn。 com / gif / lightning-gallery-20.jpg,
http://static.ddmcdn.com/gif/lightning-gallery-21.jpg
];
var size = description.length
var x = Math.floor(size * Math.random())
document.getElementById('image')。src = description [ X];
不需要jQuery。
So I have a site in which I have a description area and I have it be a random description on refresh by using the following code:
<script type="text/javascript">
var description = new Array ();
description[0] = "I can change";
description[1] = "Isn't it cool";
description[2] = "these are just to show you guys";
description[3] = "another thing";
var size = description.length
var x = Math.floor(size*Math.random())
document.write(description[x]);
</script>
Now my question, is if I wanted to have it display random images on refresh rather than a random description, how would I do it? I assume it will take a bit of jquery and maybe some appending, but I'm really not sure.
Thanks!
How about:
HTML:
<img id="image" />
JS:
var description = [
"http://static.ddmcdn.com/gif/lightning-gallery-17.jpg",
"http://static.ddmcdn.com/gif/lightning-gallery-18.jpg",
"http://static.ddmcdn.com/gif/lightning-gallery-19.jpg",
"http://static.ddmcdn.com/gif/lightning-gallery-20.jpg",
"http://static.ddmcdn.com/gif/lightning-gallery-21.jpg"
];
var size = description.length
var x = Math.floor(size*Math.random())
document.getElementById('image').src=description[x];
No jQuery necessary.