javascript:刷新时选择的随机图像 [英] javascript: random image selected on refresh

查看:122
本文介绍了javascript:刷新时选择的随机图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

code>< script type =text / javascript>
var description = new Array();
description [0] =我可以改变;
description [1] =不是很酷吗;
description [2] =这些只是为了展示你们;
description [3] =另一件事;
var size = description.length
var x = Math.floor(size * Math.random())
document.write(description [x]);
< / script>

现在我的问题是,如果我想让它在刷新时显示随机图像而不是随机描述,我该怎么做?我认为它会需要一些jQuery的,也许一些附加,但我真的不知道。



谢谢!

解决方案

如何:

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.

这篇关于javascript:刷新时选择的随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆