zoukankan      html  css  js  c++  java
  • 将类数组转换为数组的方法

     

    <script>
    var wrap= document.getElementsByClassName('wrap')[0];
    var wrapChildren = wrap.getElementsByTagName('div');
    console.log(wrapChildren); //HTMLCollection(4) [div, div, div, div]
    /*
    用原生js获取的元素级 elementcollection,nodeList
    是一个类数组
    */

    /*定义一个空数组,然后push*/
    /*let arr = [];
    for(let item of wrapChildren){
    arr.push(item);
    }
    console.log(arr);
    */

    /*Array.from 类数组-->数组*/
    /*
    var trueArray = Array.from(wrapChildren);
    console.log(trueArray);
    */

    /*...运算符*/
    /*
    var trueArray = [...wrapChildren];
    console.log(trueArray);
    */

    /*apply的第二个参数可以是数组,也可以是类数组*/
    /*
    var trueArray = [].concat.apply([],wrapChildren);
    console.log(trueArray);
    */

    var trueArray = Array.prototype.slice.call(wrapChildren);
    console.log(trueArray);
    </script>
  • 相关阅读:
    类与类之间的关系图
    UML介绍
    数据建模
    状态图
    部署图
    用例图
    业务建模
    时序图
    postgresql 维护手册
    ashx文件的使用(转)
  • 原文地址:https://www.cnblogs.com/lz932436/p/11993934.html
Copyright © 2011-2022 走看看