dom封装函数

xiaoxiao2021-02-28  11

//封装函数,返回元素e的第n层祖先元素节点 function retParent(elem. n) { while(elem && n) { elem = elem.parentElement; n --; } return elem; } var i = document.getElementsByTagName('i')[0]; //编辑函数,封装myChildren功能,解决以前部分浏览器的兼容性问题 Element.prototype.myChildren = function() { var child = this.childNodes; var len = child.length; var arr = []; for(var i =0; i < len; i ++){ if(child[i].nodeType == 1) { arr.push(child[i]) } } return arr } //自己封装hasChildren()方法,不可用children属性 Element.prototype.myChildren = function() { var child = this.childNodes; var len = child.length; var arr = [] for(var i = 0; i < len; i ++) { if( child[i].nodeType == 1 ) { return true } } return false } //封装函数,返回元素e的第n个兄弟元素节点,n为正,返回后面的兄弟元素节点,n为负,返回前面的,n为0,返回自己 function retSibling(e,n) { while(e && n) { if(n > 0) { if(e.nextElementSibling) { e = e.nextElementSibling; }else{ for(e = e.nextSibling; e.nodeType != 1; e = e = e.nextSibling); } n -- }else{ if(e.previousElementSibling) { e = e.previousElementSibling; }else{ for(e = e.previousSibling;e.nodeType != 1;e = e.previousSibling); } n ++; } } return e; }
转载请注明原文地址: https://www.6miu.com/read-2150063.html

最新回复(0)