[转帖]Mootools源码分析-15 -- Element-2

本文介绍了一个用于MooTools框架的DOM操作方法,包括获取当前节点的前后同级节点、子节点及父节点等实用功能。这些方法通过模块化的方式实现,能够有效地遍历DOM树并获取所需元素。

原帖地址:http://space.flash8.net/space/?18713/viewspace-403044.html

原作者:我佛山人

 

// 模块化写法,就地执行
( function ()    {

    
/*
    步进访问父/子/兄弟节点,高度抽象,设计比较巧妙
    eleemnt 当前节点
    walk 指定步进的方式
    start 初始节点的位置
    match 匹配过滤的标签
    all 是否返回所有指定步进中包含的节点
    nocash 是否需要对节点进行扩展,默认为false,需要
    
*/
    
var  walk  =   function (element, walk, start, match, all, nocash)    {
        
// 先确定步进的起始位置
         var  el  =  element[start  ||  walk];
        
var  elements  =  [];
        
while  (el)    {
            
// 仅返回HTML Element节点和匹配指定标签的节点(在指定匹配标签时)
             if  (el.nodeType  ==   1   &&  ( ! match  ||  Element.match(el, match)))    {
                elements.push(el);
                
// 当不需要返回所有符合条件节点时立刻跳出循环
                 if  ( ! all)     break ;
            }
        
// 移动
        el  =  el[walk];
        }
        
return  (all)  ?   new  Elements(elements, {ddup:  false , cash:  ! nocash}) : $(elements[ 0 ], nocash);
    };

    Element.implement({

        
// 获取当前节点前面的同级节点
        getPrevious:  function (match, nocash)    {
            
return  walk( this ' previousSibling ' null , match,  false , nocash);
        },

        
// 获取当前节点前面所有的同级节点
        getAllPrevious:  function (match, nocash)    {
            
return  walk( this ' previousSibling ' null , match,  true , nocash);
        },

        
// 获取当前节点后面的同级节点
        getNext:  function (match, nocash)    {
            
return  walk( this ' nextSibling ' null , match,  false , nocash);
        },

        
// 获取当前节点后面所有的同级节点
        getAllNext:  function (match, nocash)    {
            
return  walk( this ' nextSibling ' null , match,  true , nocash);
        },

        
// 获取当前节点内的第一个节点
        getFirst:  function (match, nocash)    {
            
return  walk( this ' nextSibling ' ' firstChild ' , match,  false , nocash);
        },

        
// 获取当前节点内的最后一个节点
        getLast:  function (match, nocash)    {
            
return  walk( this ' previousSibling ' ' lastChild ' , match,  false , nocash);
        },

        
// 获取当前节点的上级节点
        getParent:  function (match, nocash)    {
            
return  walk( this ' parentNode ' null , match,  false , nocash);
        },

        
// 获取当前节点的所有祖先
        getParents:  function (match, nocash)    {
            
return  walk( this ' parentNode ' null , match,  true , nocash);
        },

        
// 获取当前节点的所有子节点
        getChildren:  function (match, nocash)    {
            
return  walk( this ' nextSibling ' ' firstChild ' , match,  true , nocash);
        },

        
// 判断是否包含指定子节点
        hasChild:  function (el)    {
            el 
=  $(el,  true );
            
return  ( !! el  &&  $A( this .getElementsByTagName(el.tagName)).contains(el));
        }

    });

})();

 

转载于:https://www.cnblogs.com/maapaa/articles/mootools-s-15.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值