JS数组带有一个unshift方法可以再数组前面添加若干个元素,下面是详细的代码演示
<p id="demo">Click the button to add elements to the array.</p>
<button it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}//codego.net/3/3/1/
</script>
<p><b>Note:</b> The unshift() method does not work properly in Internet Explorer 8 and earlier, the values will be inserted, but the return value will be <em>undefined</em>.</p>
执行后数组会变成下面的样子
Lemon,Pineapple,Banana,Orange,Apple,Mango
unshift在ie8及之前的版本工作有些问题,数组会插入元素,但是返回值是undefined
本文出自 “madefake145” 博客,请务必保留此出处http://10078785.blog.51cto.com/10068785/1629241
原文地址:http://10078785.blog.51cto.com/10068785/1629241