<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p id="demo0">删除最后一个元素</p> <p id="demo1">数组最后添加一个元素</p> <p id="demo2">指定索引添加元素</p> <p id="demo3">指定索引删除元素</p> <p id="demo4">创建数组并填充元素</p> <p id="demo5">数组排序</p> <p id="demo6">数组倒序</p> <button onclick="myFunction()">点我</button> <script> function myFunction() { var arr0 = ["1", "2", "3", "4"]; arr0.pop(); document.getElementById("demo0").innerHTML = arr0; var arr1 = ["1", "2"]; arr1.push("3"); document.getElementById("demo1").innerHTML = arr1; var arr2 = ["1", "3"]; arr2.splice(1, 0, "2"); document.getElementById("demo2").innerHTML = arr2; var arr3 = ["1", "4", "2", "3"]; arr3.splice(1, 1); document.getElementById("demo3").innerHTML = arr3; var arr4 = new Array(3); arr4.fill('1'); document.getElementById("demo4").innerHTML = arr4; var arr5 = [{num:3},{num:2},{num:1}]; arr5.sort(function(a,b){ return (a.num-b.num)*(-1) }); document.getElementById("demo5").innerHTML = JSON.stringify(arr5); var arr6 = [1,4,2,3]; arr6.reverse(); document.getElementById("demo6").innerHTML = arr6; } </script> </body> </html>