欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript數(shù)組前面插入元素的方法

 更新時(shí)間:2015年04月06日 12:30:28   作者:work24  
這篇文章主要介紹了JavaScript數(shù)組前面插入元素的方法,涉及javascript中unshift方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了JavaScript數(shù)組前面插入元素的方法。分享給大家供大家參考。具體如下:

JS數(shù)組帶有一個(gè)unshift方法可以再數(shù)組前面添加若干個(gè)元素,下面是詳細(xì)的代碼演示

復(fù)制代碼 代碼如下:
<!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.unshift("Lemon","Pineapple");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</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>
</body>
</html>

執(zhí)行后數(shù)組會(huì)變成下面的樣子

Lemon,Pineapple,Banana,Orange,Apple,Mango

unshift在ie8及之前的版本工作有些問題,數(shù)組會(huì)插入元素,但是返回值是undefined

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論