码迷,mamicode.com
首页 > 编程语言 > 详细

将nodeList转换为数组(兼容性)

时间:2017-07-06 13:24:54      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:pre   class   style   rip   div   new   ++   var   nodelist   

将nodeList转换为数组(兼容性)

function arrayofNodes(nodes){
    var arr = null;
    try{
        arr = Array.prototype.slice.call(nodes,0);
    }catch(ex){
        arr = new Array();
        for(var i=0,len=nodes.length; i < len; i++){
            arr.push(nodes[i]);
        }
    }
    return arr;
}

 

在IE8以前的浏览器中nodes非JScript对象而是COM对象,所以

Array.prototype.slice.call(nodes,0);

会出错

需要catch来捕获错误,然后手动创建数组

将nodeList转换为数组(兼容性)

标签:pre   class   style   rip   div   new   ++   var   nodelist   

原文地址:http://www.cnblogs.com/cnundefined/p/7125528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!