javascript数组操作

<!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>
<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;
  }
</script>
</body>
</html>

 

上一篇:Java 往一个有序的int类型数组中有序的插入一个数


下一篇:学习C语言第五天(循环结构课后练习)