javascript – 为什么DOM树oder预订,深度优先遍历?

为什么DOM树oder预订,深度优先遍历?

与其他遍历如BFT相比,此设计选择有哪些优势?

我只是在研究DOM standard并找到了前后的定义:

An object A is preceding an object B if A and B are in the same tree
and A comes before B in tree order.

An object A is following an object B if A and B are in the same tree
and A comes after B in tree order.

Just like most programming paradigms the Web platform has finite
hierarchical tree structures,simply named trees. The tree order is
preorder,depth-first traversal.

解决方法

深度优先遍历通常是最简单的遍历样式,因为您可以递归或使用显式堆栈执行此操作;广度优先需要一个队列,在某种意义上,这是一个更复杂的数据结构.但我认为答案比传统或简单更简单:(X)HTML树的深度搜索遍历导致文本节点以呈现顺序遍历.

考虑这个相对简单的HTML子树.

或者,以原始形式:

<p>Consider this <emph>relatively</emph> simple <a href="...">HTML</a> subtree</p>

作为一棵树(省略空白和属性):

<P>
                       |
      +-----------+----+----+-----+------+               
______|______   __|___   ___|__  _|_  ___|___
Consider this   <EMPH>   simple  <A>  subtree
                  |               |
              ____|_____        __|__
              relatively         HTML

深度优先遍历:

<P>,Consider this,<EMPH>,relatively,simple,<A>,HTML,subtree

广度优先遍历:

<P>,subtree,HTML

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...