如果javascript语句出错 var pos=-1; 函数随机排序(a,b){ 返回(parseInt(Math.random()*10)%2); } 功能辊(){ var myGun=新数组(); myGun[0]=“第一个插槽”; myGun[1]=“第二个插槽”; myGun[2]=“第三个插槽”; myGun[3]=“第四个插槽”; myGun[4]=“第五个插槽”; myGun[5]=“第六个插槽”; myGun.sort(randomSort.toString(); document.getElementById(“myArray”).innerHTML=“”; 对于(i=0;i

如果javascript语句出错 var pos=-1; 函数随机排序(a,b){ 返回(parseInt(Math.random()*10)%2); } 功能辊(){ var myGun=新数组(); myGun[0]=“第一个插槽”; myGun[1]=“第二个插槽”; myGun[2]=“第三个插槽”; myGun[3]=“第四个插槽”; myGun[4]=“第五个插槽”; myGun[5]=“第六个插槽”; myGun.sort(randomSort.toString(); document.getElementById(“myArray”).innerHTML=“”; 对于(i=0;i,javascript,html,Javascript,Html,试试这个: <!DOCTYPE html> <html> <head> </head> <body> <hr> <div> <div id="myPos"> </div> </div> <hr> <!--Storing my array --> <div id="myArray">

试试这个:

      <!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>
函数快照(){
如果(位置>=myGun.长度)
{
pos=pos+1;
document.getElementById(“myPos”).innerHTML=“位置:”+pos+“
”; } 其他的 { 警惕(“你松了”); } }
试试这个:

      <!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>
函数快照(){
如果(位置>=myGun.长度)
{
pos=pos+1;
document.getElementById(“myPos”).innerHTML=“位置:”+pos+“
”; } 其他的 { 警惕(“你松了”); } }
您需要将“>=”替换为“您需要将“>=”替换为“试试这个,它对我有用,但我输了。:)我已经修复了几个minior错误,并对HTML进行了一些调整,这不是问题所在。这里的问题是数组-它是第二个函数的局部变量,但不是第三个函数的局部变量-我将其作为golobal变量,但在roll中重置了它(您的原始定义看起来应该在那里重置)

第一个函数不需要a和b参数,因为它不使用它们。 我已经移动了页面标签中的所有内容,但为了可读性,您也可以在一个单独的
.js
文件中为其编写所有脚本内容:

告诉我这对你是否也有用

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

var pos=-1;
var myGun=新阵列;
函数randomSort()
{
返回parseInt(Math.random()*10)%2;
}
功能辊()
{
myGun=[];
myGun[0]=“第一个插槽”;
myGun[1]=“第二个插槽”;
myGun[2]=“第三个插槽”;
myGun[3]=“第四个插槽”;
myGun[4]=“第五个插槽”;
myGun[5]=“第六个插槽”;
myGun.sort(randomSort.toString();
document.getElementById(“myArray”).innerHTML='';
对于(i=0;i”;
document.getElementById(“myPos”).innerHTML=“Position:”+(-pos);
}
函数快照()
{
如果(位置”;
其他的
警惕(“你松了”);
返回pos;
}





试试看,它对我有效,但我失败了。:)我已经修复了几个小错误,并对HTML进行了一些调整,这不是问题所在。这里的问题是数组-它是第二个函数的局部变量,但不是第三个函数的局部变量-我将其作为pos变量,但在roll中重置它(您的原始定义看起来应该在那里重置)

第一个函数不需要a和b参数,因为它不使用它们。 我已经移动了页面标签中的所有内容,但为了可读性,您也可以在一个单独的
.js
文件中为其编写所有脚本内容:

告诉我这对你是否也有用

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

var pos=-1;
var myGun=新阵列;
函数randomSort()
{
返回parseInt(Math.random()*10)%2;
}
功能辊()
{
myGun=[];
myGun[0]=“第一个插槽”;
myGun[1]=“第二个插槽”;
myGun[2]=“第三个插槽”;
myGun[3]=“第四个插槽”;
myGun[4]=“第五个插槽”;
myGun[5]=“第六个插槽”;
myGun.sort(randomSort.toString();
document.getElementById(“myArray”).innerHTML='';
对于(i=0;i”;
document.getElementById(“myPos”).innerHTML=“Position:”+(-pos);
}
函数快照()
{
如果(位置”;
其他的
警惕(“你松了”);
返回pos;
}





什么是无效的?您是否收到了错误?它是否没有执行您期望的操作?
pos
未在任何地方定义。您可能还需要执行以下操作:1.给我们完整的代码2.给我们错误3.处理FF,以便实际获得有意义的错误消息。请包括您收到的错误消息或不正确的错误消息您正在查看的行为更多信息=>更好的答案。@Rolice是的,您可以。赋值表达式在参数中,所以它没有问题。什么不起作用?您有错误吗?它没有执行您期望的操作吗?
pos
没有在任何地方定义。您可能还需要执行以下操作:1.给我们完整的代码2.给我们错误3。在FF上工作,以便您实际获得有意义的错误消息。请包括您收到的错误消息或您看到的错误行为更多信息=>更好的答案。@Rolice是的,您可以。赋值表达式在parens中,因此没有问题。他想“如果pos大于myGun,则发出警报”,您使用
@David:除了代码中的警报在
else
语句中。但不确定OP是否将其反向。将else描述为期望的结果似乎有些奇怪。@Patrick:是的,这让我很困惑。我读了c
<!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>