<!DOCTYPE html>
  <html>
 <head>
 </head>
 <body>
 <hr>
 <div>

   <div id="myPos">

 </div>


 </div>

  <hr>
  <!--Storing my array -->
  <div id="myArray">

  </div>

  <br>
  <br/>
  <hr>

   <script type="text/javascript">
   var pos=-1;


   function randomSort(a,b){
return(parseInt(Math.random()*10)%2);
     }

   function roll(){
var myGun = new Array();
myGun[0]="First Slot";
myGun[1]="Second Slot";
myGun[2]="Third Slot";
myGun[3]="Fourth Slot";
myGun[4]="Fifth Slot";
myGun[5]="Sixth Slot";
myGun.sort(randomSort).toString();

document.getElementById("myArray").innerHTML="";



for(i=0;i<myGun.length;i++)
{
    document.getElementById("myArray").innerHTML+=myGun[i]+ "<br>";
}

    document.getElementById("myPos").innerHTML= "Position:"+ (pos=-1);
     }

function shot(){

    if(pos<myGun.length)
    {
        document.getElementById("myPos").innerHTML="Position:"+ (pos=pos+1) +"<br/>";


   }
    else
    {
       alert("you loose");
    }
       return pos;
}

     </script>

<footer>
    <button id="btnRoll" onclick="roll()">Roll</button>
    <button id="btnShot" onclick="shot()">Shot</button>
</footer>
    </body>
    </html>

最佳答案

试试这个,对我有用,但我输了。:)我已经修复了几个minior错误,并对HTML进行了一些调整,这不是问题所在。这里的问题是数组-它是第二个函数的局部变量,但不是第三个-我将它设为golobal变量pos,但在roll中重置它(您最初的定义看起来应该在那里重置)。
第一个函数不需要a和b参数,因为它不使用它们。
我已经移动了页面标记中的所有内容,但为了可读性,您也可以将所有脚本内容放在单独的.js文件中:
<script type="text/javascript" src="path to file"></script>
告诉我这对你是否也有用。

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        var pos = -1;
        var myGun = new Array;

        function randomSort()
        {
            return parseInt(Math.random() * 10) % 2;
        }

        function roll()
        {
            myGun = [];
            myGun[0]="First Slot";
            myGun[1]="Second Slot";
            myGun[2]="Third Slot";
            myGun[3]="Fourth Slot";
            myGun[4]="Fifth Slot";
            myGun[5]="Sixth Slot";
            myGun.sort(randomSort).toString();

            document.getElementById("myArray").innerHTML = '';



            for(i=0; i < myGun.length; i++)
                document.getElementById("myArray").innerHTML += myGun[i] + "<br />";

            document.getElementById("myPos").innerHTML = "Position:" + (--pos);
        }

        function shot()
        {
            if(pos < myGun.length)
                document.getElementById("myPos").innerHTML = "Position:" + (pos++) + "<br />";
            else
                alert("you loose");

            return pos;
        }

     </script>
 </head>
 <body>
    <hr />
    <div>
        <div id="myPos"></div>
    </div>

    <hr />
    <!--Storing my array -->
    <div id="myArray">

    </div>

    <br />
    <br />

    <footer>
        <input type="button" id="btnRoll" onclick="roll()" value="Roll" />
        <input type="button" id="btnShot" onclick="shot()" value="Shot" />
    </footer>
</body>
</html>

关于javascript - 如果JavaScript陈述错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6671793/

10-12 12:26
查看更多