js模仿jquery里的几个方法parent, parentUntil, children
有时工作需要, 也是接着上一章的方法, 用js模仿jquery里的几个方法parent, parentUntil, children.
function parent(node){ return node.parentNode || null; } function parentUntil(node, parentNode){ while(parent(node) != parentNode){ node = parent(node); } return parentNode; } function children(parentNode){ var children, length, nodeArray = [], i = 0; if(parentNode.nodeType === 1){ children = parentNode.children || parentNode.childNodes; //兼顾一下ff3.6以下的版本, 当然也可以不考虑了, 直接用children length = children.length; for(; i < length; i++){ if(children[i].nodeType !== 1){ continue; } nodeArray.push(children[i]); } return nodeArray; } else{ return null; } }