将nodeList转换为数组(兼容性)node
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);浏览器
会出错spa
须要catch来捕获错误,而后手动建立数组prototype