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

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條評論
主站蜘蛛池模板: 亚洲精品天堂 | 视频一区色眯眯视频在线 | 中文字幕在线日韩 | 青青青国产色视频在线观看 | 精品中文字幕一区二区三区四区 | 一级一级一级毛片免费毛片 | 亚洲区一区 | 2018一级毛片免费观看 | 欧美最猛的24k毛片视频 | 免费播放欧美毛片欧美a | 日韩中文字幕在线不卡 | 欧美午夜性刺激在线观看免费 | 日韩夜夜操| 5g影院天天爽 | 欧美成人综合在线 | 精品香蕉一区二区三区 | 亚洲综合色婷婷中文字幕 | 久久国产乱子伦精品免费看 | 日韩视频一区二区 | 日本毛片免费看 | 中文字幕一区日韩在线视频 | 日日操操操 | 日本一本在线观看 | 国产高清视频免费 | 国产夫妻久久线观看 | 纯欧美一级毛片免费 | 能在线观看的一区二区三区 | 一级a美女毛片 | 久久99精品热在线观看15 | 伊人精品视频在线 | 日本中文字幕在线观看 | 日本在线观看中文字幕 | 成人特级毛片69免费观看 | 99香蕉网| 久久久久久亚洲精品影院 | 天天干夜夜做 | 久久99热这里只有精品免费看 | 一级毛片免费高清视频 | 久久亚洲人成国产精品 | 天天做夜夜操 | 国产特黄 |