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

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條評論
主站蜘蛛池模板: 天天操天天看 | 中日韩欧美在线观看 | 久久精品在线观看 | 欧美成人香蕉网在线观看 | 99re久久资源最新地址 | 日本h在线精品免费观看 | 国产成人精品亚洲2020 | 日韩欧美精品一区二区三区 | 久久最近最新中文字幕大全 | 红色一级毛片 | 国产伦精品一区二区三区四区 | 亚洲欧美精品中文字幕 | 日韩一区二区天海翼 | 2021国产精品久久 | 国内一区亚洲综合图区欧美 | 深夜福利网站在线观看 | 天天综合在线观看 | 国产精品久久久久久久久久日本 | 在线观看理论片 | 亚洲欧洲一区二区三区在线 | 免费一级欧美片片线观看 | 日日狠狠久久偷偷四色综合免费 | 日本激情视频一区二区三区 | 性大特级毛片视频 | 成 人 黄 色 视频播放1 | 久久久国产精品免费看 | 欧美日韩在线观看视频 | 国产成+人+综合+亚洲不卡 | 五月天婷亚洲天综合网精品偷 | 国产福利一区二区在线精品 | 日本香蕉视频在线观看 | 国产成人亚洲综合a∨婷婷 国产成人亚洲综合欧美一部 | 久久公开视频 | 国产精品亚洲第一区二区三区 | 特级毛片在线大全免费播放 | 尤物视频黄 | 国产精品一区二区三区免费视频 | 国产精品香蕉在线观看不卡 | 中文字幕在线视频精品 | 一本一本久久a久久综合精品蜜桃 | 99久久日本一区二区波多野结衣 |