下面的代碼演示了JS數(shù)組的pop方法,可以用來移除數(shù)組的最后一個(gè)元素,實(shí)際上就是把數(shù)組當(dāng)成堆棧使用
<!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>
運(yùn)行結(jié)果如下:
Banana,Orange,Apple
更多信息請(qǐng)查看IT技術(shù)專欄