Javascript-DOM中NodeList对象转数组的通用方法
举例说明:
<!DOCTYPE html>
<html>
<head></head>
<body>
<ul id="ul">
<li>
123
</li>
<li>
456
</li>
<li>
789
</li>
</ul>
</body>
<script>
var _childNodes = document.getElementById("ul").childNodes;
function convertToArray(nodes) {
var array = null;
try {
array = Array.prototype.slice.call(nodes, 0); // 针对非IE浏览器
} catch (ex) {
array = new Array();
for (var i = 0, len = nodes.length; i < len; i++) {
array.push(nodes[i]);
}
}
return array;
}
console.log(convertToArray(_childNodes));
</script>
</html>