我们可以通过JS数组的splice方法在执行的位置插入新的元素,非常简单
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to add elements to the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
上面的代码执行后输出如下结果
Banana,Orange,Lemon,Kiwi,Apple,Mango
