Movatterモバイル変換


[0]ホーム

URL:


  1. Веб-технологии для разработчиков
  2. Интерфейсы веб API
  3. Document
  4. Document.createTreeWalker()

This page was translated from English by the community.Learn more and join the MDN Web Docs community.

View in EnglishAlways switch to English

Document.createTreeWalker()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨июль 2015 г.⁩.

Вызов методаDocument.createTreeWalker() возвращает новый объект классаTreeWalker.

Синтаксис

document.createTreeWalker(root, whatToShow[, filter[, entityReferenceExpansion]]);

Параметры

root

корневой узелNode дляTreeWalker. Чаще всего это элемент принадлежащий document.

whatToShowНеобязательный

Aunsigned long representing a bitmask created by combining the constant properties ofNodeFilter. It is a convenient way of filtering for certain types of node. It defaults to0xFFFFFFFF representing theSHOW_ALL constant.

КонстантаЧисловое значениеОписание
NodeFilter.SHOW_ALL-1 (that is the max value ofunsigned long)Показывать все узлы.
NodeFilter.SHOW_ATTRIBUTEУстарело2Shows attributeAttr nodes. This is meaningful only when creating aTreeWalker with anAttr node as its root; in this case, it means that the attribute node will appear in the first position of the iteration or traversal. Since attributes are never children of other nodes, they do not appear when traversing over the document tree.
NodeFilter.SHOW_CDATA_SECTIONУстарело8ShowsCDATASection nodes.
NodeFilter.SHOW_COMMENT128ShowsComment nodes.
NodeFilter.SHOW_DOCUMENT256ShowsDocument nodes.
NodeFilter.SHOW_DOCUMENT_FRAGMENT1024ShowsDocumentFragment nodes.
NodeFilter.SHOW_DOCUMENT_TYPE512ShowsDocumentType nodes.
NodeFilter.SHOW_ELEMENT1ShowsElement nodes.
NodeFilter.SHOW_ENTITYУстарело32ShowsEntity nodes. This is meaningful only when creating aTreeWalker with anEntity node as its root; in this case, it means that theEntity node will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.
NodeFilter.SHOW_ENTITY_REFERENCEУстарело16ShowsEntityReference nodes.
NodeFilter.SHOW_NOTATIONУстарело2048ShowsNotation nodes. This is meaningful only when creating aTreeWalker with aNotation node as its root; in this case, it means that theNotation node will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.
NodeFilter.SHOW_PROCESSING_INSTRUCTION64ShowsProcessingInstruction nodes.
NodeFilter.SHOW_TEXT4ShowsText nodes.
filterНеобязательный

ANodeFilter, that is an object with a methodacceptNode, which is called by theTreeWalker to determine whether or not to accept a node that has passed thewhatToShow check.

entityReferenceExpansionНеобязательный

ABoolean flag indicating if when discarding anEntityReference its whole sub-tree must be discarded at the same time.

Return value

A newTreeWalker object.

Example

The following example goes through all nodes in the body, reduces the set of nodes to elements, simply passes through as acceptable each node (it could reduce the set in theacceptNode() method instead), and then makes use of tree walker iterator that is created to advance through the nodes (now all elements) and push them into an array.

js
var treeWalker = document.createTreeWalker(  document.body,  NodeFilter.SHOW_ELEMENT,  {    acceptNode: function (node) {      return NodeFilter.FILTER_ACCEPT;    },  },  false,);var nodeList = [];while (treeWalker.nextNode()) nodeList.push(treeWalker.currentNode);

Спецификации

Specification
DOM
# dom-document-createtreewalker

Совместимость с браузерами

Смотрите также

  • The interface of the object it creates:TreeWalker.

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp