1、在数组的开头添加新元素 - unshift()
<script>
function
myFunction()
{
var
fruits = [
"Banana"
,
"Orange"
,
"Apple"
,
"Mango"
];
fruits.unshift(
"Lemon"
,
"Pineapple"
);
var
x=document.getElementById(
"demo"
);
x.innerHTML=fruits;
}
</script>
测试结果:
Lemon,Pineapple,Banana,Orange,Apple,Mango
2、在数组的第2位置添加一个元素 - splice()
<script>
function
myFunction()
{
var
fruits = [
"Banana"
,
"Orange"
,
"Apple"
,
"Mango"
];
fruits.splice(2,0,
"Lemon"
,
"Kiwi"
);
var
x=document.getElementById(
"demo"
);
x.innerHTML=fruits;
}
</script>
测试结果:
Banana,Orange,Lemon,Kiwi,Apple,Mango
3、数组的末尾添加新的元素 - push()
<script>
var
fruits = [
"Banana"
,
"Orange"
,
"Apple"
,
"Mango"
];
function
myFunction()
{
fruits.push(
"Kiwi"
)
var
x=document.getElementById(
"demo"
);
x.innerHTML=fruits;
}
</script>
测试结果:
Banana,Orange,Apple,Mango,Kiwi
以上文档参考来源:
http://www.jb51.net/article/73867.htm
实际应用:
var head_data = opt.head;//注意,如果有多层嵌套时要分别获取元素的根
var head_icon = head_data.icon;
head_icon.splice(3,0,{
icon: 'fi fi-clothes',
title: '主题',
onclick: function(){
layer.open({
type: 2,
title: '切换皮肤',
shadeClose: false,
shade: 0,
area: ['380px', '40%'],
content: 'url' //iframe的url
});
}
});