//函数封装
//实现获取某个节点的所有子元素节点,不能使用children属性
var div=document.getElementsByTagName('div')[0];
function retElementChild(node){
var temp={
length:0,
push:Array.prototype.push,
splice:Array.prototype.splice
};
//var temp=[];
var len=node.childNodes.length;
for(var i=0;i<len;i++){
if(node.childNodes[i].nodeType===1){
temp.push(node.childNodes[i]);
}
}
return temp;
}
console.log(retElementChild(div));

本文介绍了一种不使用children属性获取某个节点所有子元素的方法,通过遍历childNodes并筛选出nodeType为1的元素,将其存入自定义数组中返回。
1414

被折叠的 条评论
为什么被折叠?



