1

这是我的脚本可能更容易解释,如果你能看到它,我想用按钮重置数组

 <body> 
  <script type="text/javascript"> 
   var number = []; 

   function myFunction() 
   { 
     var x = document.getElementById("box"); 
     number.push(document.getElementById("input").value); 
     x.innerHTML = number.join('<br/>');  
   } 


  </script> 
 <form> 
  <input id="input" type=text> 
   <input type=button onclick="myFunction()" value="Add to list"/>  
  </form>

 <div id="box" style="border:1px solid black;width:150px;height:150px;overflow:scroll">  
  </div> 
<form>
<input type=button onclick="number.splice(0)" value="Reset" />
</form>
</body> 
</html>
4

2 回答 2

1

是的。从函数中调用它。

编辑: splice 与 IE 有问题。使用 array.length = 0 代替

<body> 
    <script type="text/javascript"> 
        var number = []; 

        function myFunction() { 
            var x = document.getElementById("box"); 
            number.push(document.getElementById("input").value); 
            x.innerHTML = number.join('<br/>');  
        } 

        function myReset() {
            //number.splice(0);
            number.length = 0;
            var x = document.getElementById("box"); 
            x.innerHTML = "";
            x.innerHTML = number.join('<br/>');  
        }
    </script> 

    <form> 
        <input id="input" type=text> 
        <input type=button onclick="myFunction()" value="Add to list"/>  
    </form>

    <div id="box" style="border:1px solid black;width:150px;height:150px;overflow:scroll">  
    </div> 
    <form>
        <input type=button onclick="myReset()" value="Reset" />
    </form>
</body> 
于 2012-09-28T02:36:26.267 回答
0

尽管丑陋,但onclick="number.splice(0)"有效:http: //jsfiddle.net/39KZ9/20/

于 2012-09-27T23:25:59.750 回答