javascript随机var数组图像

时间:2014-11-20 15:30:09

标签: javascript

我有以下代码:

<script src="js/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="js/html5placeholder.jquery.js" type="text/javascript"></script>

<!--[if IE]><script type="text/javascript" src="js/excanvas.js"> </script><![endif]-->
<script src="js/general.js" type="text/javascript"></script>
<script language="JavaScript"> 
function foo(n)
{
    var a = 123456789;
    a = a.toString();
    var result = "";
    e = document.getElementById("kq");
    for(i = 0; i < a.length; i++)
    {
        result += "<img src=\"number/a"+a[i]+".png\" />";

    }
    e.innerHTML = result;

}


var solanMax = 10;
var n = 0;
function foo2()
{
        solan = 0;
        result = "";
        result += "<img src=\"number/a"+n+".png\" />";
        if(n == 9)
            n -= 9;
        if(n >= 0)
            n++;
        e0 = document.getElementById("kq0");
         e1 = document.getElementById("kq1");
          e2 = document.getElementById("kq2");
           e3 = document.getElementById("kq3");
            e4 = document.getElementById("kq4");
             e5 = document.getElementById("kq5");
              e6 = document.getElementById("kq6");
               e7 = document.getElementById("kq7");
                e8 = document.getElementById("kq8");
                 e9 = document.getElementById("kq9");

        e0.innerHTML = result;
        e1.innerHTML = result;
        e2.innerHTML = result;
        e3.innerHTML = result;
        e4.innerHTML = result;
        e5.innerHTML = result;
        e6.innerHTML = result;
        e7.innerHTML = result;
        e8.innerHTML = result;
        e9.innerHTML = result;
        solan ++;
        T = setTimeout('foo2('+n+')',solan); 
}
function Stop(){
setTimeout('clearTimeout(T)',1000);

}
</script> 
<input type=button value='Lottery Number Picker' onClick="foo2();"> 
<span id="kq0" class=a1>Result</span> 
<span id="kq1" class=a1>Result</span> 
<span id="kq2" class=a1>Result</span> 
<span id="kq3" class=a1>Result</span> 
<span id="kq4" class=a1>Result</span> 
<span id="kq5" class=a1>Result</span> 
<span id="kq6" class=a1>Result</span> 
<span id="kq7" class=a1>Result</span> 
<span id="kq8" class=a1>Result</span> 
<span id="kq9" class=a1>Result</span> 
<br>
<input type=button value='Stop' onClick="Stop();">

我想按“停止”按钮,返回的数量对于所有预设都是相同的 演示:http://group-cast.com/Binhchon/test.php 非常感谢,我是越南人,所以我的英语有点不好

2 个答案:

答案 0 :(得分:0)

我不确定你到底想要什么。但是,当您为彩票生成数字时,我假设您希望数字是随机的。您可以添加新功能getImageForRandomNum并像这样更改foo2功能......

function getImageForRandomNum(){
      var n = Math.floor((Math.random() * 10));
      result = "<img src=\"number/a"+n+".png\" />";
      return result;
}

function foo2()
{
        e0 = document.getElementById("kq0");
         e1 = document.getElementById("kq1");
          e2 = document.getElementById("kq2");
           e3 = document.getElementById("kq3");
            e4 = document.getElementById("kq4");
             e5 = document.getElementById("kq5");
              e6 = document.getElementById("kq6");
               e7 = document.getElementById("kq7");
                e8 = document.getElementById("kq8");
                 e9 = document.getElementById("kq9");

        e0.innerHTML = getImageForRandomNum();
        e1.innerHTML = getImageForRandomNum();
        e2.innerHTML = getImageForRandomNum();
        e3.innerHTML = getImageForRandomNum();
        e4.innerHTML = getImageForRandomNum();
        e5.innerHTML = getImageForRandomNum();
        e6.innerHTML = getImageForRandomNum();
        e7.innerHTML = getImageForRandomNum();
        e8.innerHTML = getImageForRandomNum();
        e9.innerHTML = getImageForRandomNum();
        solan ++;
        T = setTimeout('foo2('+n+')',solan); 
}

答案 1 :(得分:0)

由于您在页面上有jquery,我会将您的span html更改为此

<span id="kq0" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq1" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq2" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq3" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq4" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq5" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq6" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq7" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq8" class="a1"><img src="" alt="" class="randomNum"></span> 
<span id="kq9" class="a1"><img src="" alt="" class="randomNum"></span>

然后将你的foo2功能改为

function foo2() {

   $(".randomNum").each(function(){
      var ran = Math.floor(Math.random() * 9 + 1);
      var img = "number/a" + ran + ".png";
      $(this).attr('src', img);
   });
   T = setTimeout(foo2, 500);
}