亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

DOM TreeWalker - Tales of a Developer Advoca

系統 1866 0

DOM TreeWalker - Tales of a Developer Advocate

DOM TreeWalker

Aug 8 th , 2010

I really wanted to get a reference to Walker Texas Ranger in to the title, but I really couldn’t think of anything that cool. If you can think of a great Chuck Norris reference leave a comment, I am all chins!

It always amazes me that there is so much to HTML that is still not being exploited by developers.

One pattern I see regularly is recursive descent through the DOM to find particular TEXT nodes that contain a given string so that the container element can be manipulated.

It is not that recursion is slow, if your DOM is complex enough you could hit stack overflow errors (although it is pretty unlikely), it is that there are a lot of edge cases when parsing the DOM that you need to code in.

A little known DOM function is available that makes developing applications that need to scan the DOM easy. It is called Tree Walker, created through the createTreeWalker function on the document.

You can create a tree walker very quickly using the following Javascript:

            document.createTreeWalker(document.body, NODE_FILTER.SHOW_TEXT, 
            
              function
            
            (node) { 
            
              return
            
             NodeFilter.FILTER_ACCEPT; }, 
            
              false
            
            );


            
              while
            
            (treeWalker.nextNode()) console.log(treeWalker.currentNode);
          

The above code is given a root node of document.body, a filter of what to show (only Text Nodes in our case), and a function that returns if the node should be returned (essentially a filter).

An interesting point to note is that the Filter function is only called when iterating over the treeWalker.

This is actually a really cool feature, the currentNode property of the Tree Walker contains DOM objects, so you can start to do some really advanced processing, you could highlight the current node, replace its text or remove it – really anything you want. This is significantly simpler than managing the recursion yourself.

As a more concrete example, lets use this to find all twitter user names on a page and then automatically make these a twitter link. It could be done using recursion pretty simply, but I need something fun to show you.

            
              var
            
             re = 
            
              new
            
             RegExp(); 
            
              // This isn't accurate RE
            
            
re.compile(
            
              
                "
              
              
                @([A-Za-z0-9_]*)
              
              
                "
              
            
            );

            
              var
            
             walker = document.createTreeWalker(
  document.body,
  NodeFilter.SHOW_TEXT,
  
            
              function
            
            (node) {
    
            
              var
            
             matches = node.textContent.match(re);

    
            
              if
            
            (matches) { 
      
            
              return
            
             NodeFilter.FILTER_ACCEPT;
    } 
            
              else
            
             {
      
            
              return
            
             NodeFilter.FILTER_SKIP;
    }
  },
  
            
              false
            
            );


            
              var
            
             nodes = [];


            
              while
            
            (walker.nextNode()) {
  nodes.push(walker.currentNode);
}


            
              for
            
            (
            
              var
            
             i = 
            
              0
            
            ; node=nodes[i] ; i++) {
  node.parentNode.innerHTML = node.parentNode.innerHTML.replace(re, 
            
              
                "
              
              
                @
                
                  $1
                
              
              
                "
              
            
            ) }
          

A live example is on my sample site

The theory is, that User-Agents can optimize the access to the DOM better than you can recursively descend through the DOM. So, where would I use this? The first thing that springs to mind is that it is ideal for Chrome extensions. Many Chrome extensions traverse the DOM looking for pieces of text, or particular patterns inside nodes that aren’t available via CSS Selectors.

More information can be found on Mozilla’s Developer site

Aug 8 th , 2010

DOM TreeWalker - Tales of a Developer Advocate


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 四虎永久免费网站免费观看 | 久久99热成人精品国产 | 亚洲综合色就色手机在线观看 | 中国美女一级a毛片录像在线 | 九九影院理论片私人影院 | 不卡伦理 | 欧美怡红院免费全部视频 | 国产成人精品久久二区二区 | 天天碰夜夜 | 日本人一级毛片免费完整视频 | 久久九九有精品国产23百花影院 | 九热爱视频精品视频 | 99在线看| 久久久噜噜噜 | 色偷偷尼玛图亚洲综合 | 欧美日韩在线观看免费 | 中文字幕久久精品波多野结 | 久久综合九色综合97_ 久久久 | 国产91在线免费观看 | 成人国产精品视频频 | 日本一级黄色毛片 | 毛片直接看 | 国产一区二区网站 | 国产精品综合视频 | 欧美破处在线 | 欧美精品一区在线看 | 亚洲自拍第二页 | 欧美影院一区 | 久久一级黄色片 | 欧美成人h版影片在线观看 欧美成人h精品网站 | 婷婷资源综合 | 成人黄色小视频 | 一级毛片在线免费播放 | 亚洲精品9999久久久久 | 福利小视频在线 | 日韩一区二区超清视频 | 久 在线播放 | 婷婷婷色| 超清中文乱码精品字幕在线观看 | 久久一本 | 经典国产乱子伦精品视频 |