【发布时间】:2012-10-07 12:13:22
【问题描述】:
我已经解决了这个问题的第一部分,我可以像这样从数组中选择一个随机元素
function setImage()
{
var images = ['anemone.gif', 'ball.gif', 'crab.gif', 'fish2.gif', 'gull.gif', 'jellyfish.gif', 'moon.gif', 'sail.gif', 'shell.gif', 'snail.gif', 'sun.gif', 'sunnies.gif', 'whale.gif'];
var slots = [document.getElementById('slot0'), document.getElementById('slot1'), document.getElementById('slot2')];
document.getElementById('slot0').src = images[Math.floor(Math.random() * images.length)];
document.getElementById('slot1').src = images[Math.floor(Math.random() * images.length)];
document.getElementById('slot2').src = images[Math.floor(Math.random() * images.length)];
alert(images.indexOf(document.getElementById('slot2')));
}
但是第二行没有给我正确的元素索引,我不知道还有什么方法可以找到它?
【问题讨论】:
-
在
slot2中设置索引但是slot0的警告内容对吗?
标签: javascript arrays random