从图像链接的JavaScript数组已经来自数据库的Javascript显示3随机图像 [英] Display 3 random images in Javascript from an javascript array of image links that have come from a database
问题描述
我奋力把3个随机图像(不重复)我的JavaScript数组的和独立的div内它们打印在屏幕上。
I am struggling to take 3 random images (No repeat) out of my Javascript array and print them on screen within separate Divs.
我的Javascript阵列是由包含链接/服务器上的图像的位置的数据库填充。
My Javascript array is being populated by a database which contains the links/location of the images on the server.
目前我的网页看起来是这样的:
Currently my page looks like this:
<html>
<head>
</head>
<body>
<div></div>
<script>
var items = [
{name:'Coch',image:'upload/coch.png'},{name:'Glas',image:'upload/glas.png'}, {name:'Melyn',image:'upload/melyn.png'},{name:'Ci',image:'upload/dog.jpg'}, {name:'Cath',image:'upload/cath.jpg'},{name:'Gwyrdd',image:'upload/gwyrdd.png'},{name:'Un',image:'upload/un.jpg'},{name:'Dau',image:'upload/dau.jpg'},{name:'Tri',image:'upload/tri.jpg'},{name:'Bochdew',image:'upload/bochdew.jpg'},{name:'Piws',image:'upload/piws.png'} ];
for (var i = 0; i < items.length; i += 1) {
document.getElementsByTagName('div')[0].innerHTML += items[i].name + " / " + items[i].image + "<br />\n";
}
</script>
</body>
</html>
这code只是需要从数据库中图像链接并将其插入到一个数组javascipt的。 (并打印屏幕上的链接的时刻)
This code simply takes the image links from the database and inserts them into a javascipt array. (And prints the links on screen at the moment)
谁能给我如何得到这个工作任何帮助吗?
以3个随机图像链接从阵列(无重复的),并在3个不同的div在屏幕上显示的3张照片。
Can anyone give me any help on how to get this to work? Take 3 random images links from that array (No duplication's), and display the 3 images on screen in 3 different divs.
我不擅长与所有javascipt的任何code例子将是巨大的。
I am not good with javascipt at all and any code examples would be great.
感谢您事先的任何答复。
Thank You in advance for any replies.
推荐答案
洗牌阵列,然后取前三个项目。
在这里你可以找到一个不错的功能洗牌数组:
如何随机JavaScript数组?
Shuffle your array and then take the first three items. Here you can find a nice function to shuffle arrays: How to randomize a javascript array?
这篇关于从图像链接的JavaScript数组已经来自数据库的Javascript显示3随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!