zoukankan      html  css  js  c++  java
  • 递归 访问树节点

     1 //定义 walk_the_DOM 函数,它从某个给定的节点开始,按HTML源码中的顺序访问该树的每个节点
     2 //它会调用一个函数,并依次传递每个节点给它。walk_the_DOM 调用自身去处理每一个子节点。
     3 var walk_the_DOM = function walk(node, func) {
     4         func(node);
     5         node = node.firstChild;
     6         while (node) {
     7             walk(node, func);
     8             node = node.nextSibling;
     9         }
    10     };
    11 
    12 //定义 getElementsByAttribute 函数。它取得一个属性名称字符串和一个可选的匹配值。
    13 //它调用 walk_the_DOM ,传递一个用来查找节点属性名的函数。
    14 //匹配的节点会累积到一个结果数组中。
    15 var getElementsByAttribute = function(att, value) {
    16         var results = [];
    17 
    18         walk_the_DOM(document.body, function(node) {
    19             var actual = node.nodeType === 1 && node.getAttribute(att);
    20             if (typeof actual === 'string' && (actual === value || typeof value !== 'string')) {
    21                 results.push(node);
    22             }
    23         });
    24         return results;
    25     };

    简单的递归方法

  • 相关阅读:
    leetcode之Search in Rotated Sorted Array
    leetcode之Search Insert Position2
    leetcode之Search Insert Position
    二分查找之Search for a Range
    leetcode之Two Sum
    leetcode之Spiral Matrix II
    leetcode之Spiral Matrix
    杨辉三角
    周计划1[7.22~7.28]
    英美音的比较
  • 原文地址:https://www.cnblogs.com/qzsonline/p/2583758.html
Copyright © 2011-2022 走看看