下面的代碼演示了JS數組的pop方法,可以用來移除數組的最後一個元素,實際上就是把數組當成堆棧使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html> <html> <body> <p id="demo"> Click the button to remove the last array element. </p> <button onclick="myFunction()">Try it</button> <script> var fruits = ["Banana","Orange","Apple","Mango"]; function myFunction() { fruits.pop(); var x=document.getElementById("demo"); x.innerHTML=fruits; } </script> </body> </html>運行結果如下:
Banana,Orange,Apple