zoukankan      html  css  js  c++  java
  • js向数组中插入元素

    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
    });

    }
    });

  • 相关阅读:
    大哥带的MSsql注入(SQL Server)--预习
    大哥带我们的mysql注入 基于时间的盲注
    大哥带我们的mysql注入 基于bool的盲注
    sqli-labs(22)
    sqli-labs(21)
    sqli-labs(20)
    sqli-labs(19)
    kali文件执行的权限不够解决办法
    DVWA--XSS(反射型)
    Updatexml函数再mysql中的作用
  • 原文地址:https://www.cnblogs.com/seeusmile-cnblog/p/7606326.html
Copyright © 2011-2022 走看看