源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JavaScript数组前面插入元素的方法

  • 时间:2021-08-29 02:26 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JavaScript数组前面插入元素的方法
本文实例讲述了JavaScript数组前面插入元素的方法。分享给大家供大家参考。具体如下: JS数组带有一个unshift方法可以再数组前面添加若干个元素,下面是详细的代码演示
[u]复制代码[/u] 代码如下:
<!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>
执行后数组会变成下面的样子 Lemon,Pineapple,Banana,Orange,Apple,Mango unshift在ie8及之前的版本工作有些问题,数组会插入元素,但是返回值是undefined 希望本文所述对大家的javascript程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部