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

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條評論
主站蜘蛛池模板: 亚洲激情小视频 | 国产视频中文字幕 | 精品四虎免费观看国产高清午夜 | 爱爱片免费看 | 日韩欧美成末人一区二区三区 | 天天干妹子 | 国产1区精品 | 男任天堂2021 | 国产 日韩 一区 | 玖玖爱精品 | 一级毛片免费视频观看 | 日韩 在线视频精品 | 日日人人| 99色影院 | 国产精品资源 | 毛片啪啪视频 | 九天玄帝诀高清300集免费观看 | 色老头网站久久网 | 免费视频一区二区性色 | 亚洲无线码一区在线观看 | www.色午夜 | 日本免费不卡 | 羞羞色院91蜜桃在线观看 | 亚洲欧美日韩中文v在线 | 综合色久 | 四虎影视最新网址 | 国产精品2020在线看亚瑟 | 二区三区 | 成人小视频免费 | 国产成人啪午夜精品网站男同 | 色狠狠综合 | 亚洲欧美日韩久久精品第一区 | 黄色毛片免费看 | 四虎影视最新网址 | 国产欧美精品一区二区三区四区 | 欧美午夜艳片欧美精品 | 国产高清自拍一区 | 婷婷91| 成人中文字幕一区二区三区 | 99精品久久精品一区二区 | 亚洲成人高清在线 |