从JS遍历DOM树学算法

栏目: JavaScript · 发布时间: 7年前

内容简介:从简单的需求/面试题谈起自定义一个方法去检查DOM中某个ID的元素。类似getElementById.先推荐阅读下:

从简单的需求/面试题谈起

自定义一个方法去检查DOM中某个ID的元素。类似getElementById.

先推荐阅读下:

广度优先 .

深度优先搜索

// HTML结构如下

<div class="wrapper">
    <section class="header">
        <div class="logo"></div>
    </section>
    <section class="main">
        <div class="sidebar">
            <ul class="menu">
                <li class='li'>
                    <a href="" id='demo'>li1-a</a>
                </li>
                <li class='li'>
                    <a href="">li2</a>
                </li>
            </ul>
        </div>
    </section>
    <section class="footer">
        <div class="copyright"></div>
    </section>
</div>
复制代码

简单画了下DOM节点(只考虑元素节点)图:

从JS遍历DOM树学算法

代码实现

  • 深度优先, 递归实现
const cusGetElementByIdByDFS = function (parentNode, id) {
    // 深度优先, 递归实现
    if (parentNode) {
        let target = null;
        const children = Array.from(parentNode.children);
        if (parentNode.id === id) {
            return parentNode;
        }
        for (let i = 0; i < children.length; i++) {
            target = cusGetElementByIdByDFS(children[i], id);
            if (target) {
                return target;
            }
        }
    }
    return null;
}

复制代码
// 测试代码
console.log(cusGetElementByIdByDFS(document.querySelector('.wrapper') , 'demo'))
复制代码
  • 深度优先, 非递归实现, 使用栈
const cusGetElementByIdByDFS2 = function (parentNode, id) {
    if (!parentNode) {
        return null;
    }
    // 深度优先, 非递归实现, 使用栈
    let stack = [];
    if (parentNode.id === id) {
        return parentNode;
    }
    for (let i = parentNode.children.length; i > 0; i--) {
        stack.push(parentNode.children[i - 1]);
    }
    while (stack.length) {
        let node = stack.pop();
        if (node.id === id) {
            return node;
        } else {
            if (node.children.length > 0) {
                stack = Array.from(node.children).concat(stack);
            }
        }
    }
}
复制代码
// 测试代码
console.log(cusGetElementByIdByDFS2(document.querySelector('.wrapper') , 'demo'))
复制代码
  • 广度优先 非递归实现
const cusGetElementByIdByBFS = function (parentNode, id) {
    // 广度优先 非递归实现
    // 队列的思想: 采用出队的方式遍历节点,如果遍历到的节点有子节点,则将子节点入队
    const layer = []; // 按照顺序存放每个层级的每个节点
    if (parentNode) {
        // 初始化layer
        // 节点深度从父节点开始算起
        layer.push({
            node: parentNode,
            depth: 1
        });
        while (layer.length > 0) {
            const root = layer.shift(); // 出队
            if (root.node.id === id) {
                return root; // 包括对应节点和节点深度
            } else {
                if (root.node.children.length > 0) {
                    Array.from(root.node.children).forEach(node => {
                        layer.push({
                            node,
                            depth: root.depth + 1
                        })
                    })
                }
            }
        }
    }
    return null;
}
复制代码
// 测试代码
console.log(cusGetElementByIdByBFS(document.querySelector('.wrapper') , 'demo'))
复制代码
从JS遍历DOM树学算法

后续看细下算法书再做补充, 初次学习有勘误请指出。


以上所述就是小编给大家介绍的《从JS遍历DOM树学算法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Computers and Intractability

Computers and Intractability

M R Garey、D S Johnson / W. H. Freeman / 1979-4-26 / GBP 53.99

This book's introduction features a humorous story of a man with a line of people behind him, who explains to his boss, "I can't find an efficient algorithm, but neither can all these famous people." ......一起来看看 《Computers and Intractability》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码