jQuery javascript:刷新时选择的随机图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16428154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:07:02  来源:igfitidea点击:

javascript: random image selected on refresh

javascriptjqueryhtmlimagerandom

提问by Ryan Saxe

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.

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

Thanks!

谢谢!

回答by urban_raccoons

How about:

怎么样:

HTML:

HTML:

<img id="image" />

JS:

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.

不需要jQuery。

回答by Mohammad Adil

http://jsfiddle.net/mohammadAdil/SvswX/

http://jsfiddle.net/mohammadAdil/SvswX/

<img id='random'/>

script -

脚本 -

var image = new Array ();
image[0] = "http://placehold.it/20";
image[1] = "http://placehold.it/30";
image[2] = "http://placehold.it/40";
image[3] = "http://placehold.it/50";
var size = image.length
var x = Math.floor(size*Math.random())

$('#random').attr('src',image[x]);

回答by Amin Arjmand

You can have links with images with this code :
At first insert this into your page :

您可以使用此代码链接图像:
首先将其插入您的页面:

<a id='LinksRef'>
<img id='BannersSrc'>
</a>

Then

然后

var Banners= [
  "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 Links= [
  "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 = Banners.length
var x = Math.floor(size*Math.random())
document.getElementById('BannersSrc').src=Banners[x];
document.getElementById('LinksRef').href=Links[x];