从阵列加载图像

时间:2013-11-06 02:37:50

标签: javascript jquery arrays image

我正在制作一个有30个不同问题的旗帜游戏。我有30个不同的标志,不能随机显示它们。

我的页面如下:

<div data-role="page" id="page2" align="center">
 <div data-role="header">
<h1>Name The Flag</h1>
 </div>

 <div data-role="content">
 <div id="outerDiv">    

 <div class="innerDiv">
    <div id="imgFlags">     
 </div>

 <br /><div id="capitalDisplay">Capital City: </div>
 </div>
 </div>

           
     ANSWER1                                           ANSWER2                                                             ANSWER3                                                             answer4                                                                                                                                             
<div data-role="footer">
    <h4>Andrew</h4>
</div>

我的功能看起来像这样:

for (var i = 0 ; i < imgFlags.length ; i++)
imgFlags[i] = new Image();

var imgFlags = ['images/01.png', 'images/02.png', 'images/03.png', 'images/04.png', 'images/05.png', 'images/06.png', 'images/07.png', 'images/08.png', 'images/09.png', 'images/10.png', 'images/11.png', 'images/12.png', 'images/13.png', 'images/14.png', 'images/15.png', 'images/16.png', 'images/17.png', 'images/18.png', 'images/19.png', 'images/20.png', 'images/21.png', 'images/22.png', 'images/23.png', 'images/24.png', 'images/25.png', 'images/26.png', 'images/27.png', 'images/28.png', 'images/29.png', 'images/30.png'];

有人请帮忙!

1 个答案:

答案 0 :(得分:0)

var imgFlags = ['images/01.png', 'images/02.png', 'images/03.png', 'images/04.png', 'images/05.png', 'images/06.png', 'images/07.png', 'images/08.png', 'images/09.png', 'images/10.png', 'images/11.png', 'images/12.png', 'images/13.png', 'images/14.png', 'images/15.png', 'images/16.png', 'images/17.png', 'images/18.png', 'images/19.png', 'images/20.png', 'images/21.png', 'images/22.png', 'images/23.png', 'images/24.png', 'images/25.png', 'images/26.png', 'images/27.png', 'images/28.png', 'images/29.png', 'images/30.png'];  
var randomImage=Math.floor(Math.random()*imgFlags.length);  
document.addEventListener('DOMContentLoaded',function(){  
document.getElementById('imgFlags').innerHTML=  "<img src ='"+ imgFlags[randomImage] +"' />";
},false);  

用上面的代码替换你的javascript并研究它..