下面的代码演示了JS数组的pop方法,可以用来移除数组的最后一个元素,实际上就是把数组当成堆栈使用
<!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
更多信息请查看IT技术专栏