W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript 数组方法</h1> <h2>splice()</h2> <p>splice() 方法向数组添加新元素,并返回包含已删除元素(如果有)的数组。</p> <button onclick="myFunction()">试一试</button> <p id="demo1"></p> <p id="demo2"></p> <p id="demo3"></p> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = "原数组:<br> " + fruits; function myFunction() { var removed = fruits.splice(2, 2, "Lemon", "Kiwi"); document.getElementById("demo2").innerHTML = "新数组:<br>" + fruits; document.getElementById("demo3").innerHTML = "已删除项:<br> " + removed; } </script> </body> </html>