Traverse node APIs

Source: Internet
Author: User
ArticleDirectory
    • Nodeiterator

Internet Explorer has been used for a long time, and many new APIs are unavailable. For example, in dom1.0, we all know the nextsibling, previussibling, childnodes, firstchild, lastchild, parentnode, and children attributes. However, the traversal API was standardized a year ago, it is classified into a series with nextelementsibling, previuselementsibling, firstelementchild, and lastelementchild. However, standard browsers also support more powerful traversal and provide filtering functions.

Nodeiterator

This is an object. to use it, you need to call the document. createnodeiterator method.

 
VaR nodeiterator = Document. createnodeiterator (root, whattoshow, filter, entityreferenceexpansion );

The first parameter: Which node to traverse, including itself.

The second parameter is a number used to determine the type of nodes to traverse.

Nodefilter. show_all =-1nodefilter. show_element = 1nodefilter. show_attribute = 2nodefilter. show_text = 4nodefilter. show_cdata_section = 8nodefilter. show_entity_reference = 16nodefilter. show_entity = 32nodefilter. show_processing_instruction = 64nodefilter. show_comment = 128nodefilter. show_document = 256nodefilter. show_document_type = 512nodefilter. show_document_fragment = 1024nodefilter. show_notation = 2048

Third parameter: an object in a very fixed format, or simply null.

 
{Acceptnode: function (node) {return nodefilter. filter_accept ;},// nodefilter. filter_accept can be changed to nodefilter. filter_reject or nodefilter. filter_skip.

The fourth parameter is a Boolean value. If it is set to false, it is correct.

An example is to obtain all element nodes of the body, including the body itself.

VaR nodeiterator = Document. createnodeiterator (document. Body, nodefilter. show_element, {acceptnode: function (node) {return nodefilter. filter_accept; }}, false );

For more information, click here

Treewalker

The usage is basically the same as that of nodeiterator.

 
VaR treewalker = Document. createtreewalker (root, whattoshow, filter, entityreferenceexpansion );

In one example, all element nodes of the body are obtained, but the body itself is not included.

VaR treewalker = document. createtreewalker (document. body, nodefilter. show_element, {acceptnode: function (node) {return nodefilter. filter_accept ;}}, false); var nodelist = new array (); While (treewalker. nextnode () nodelist. push (treewalker. currentnode); alert (nodelist)

For more information, click here or
Here

// Get all element, comment, and text nodes in the document
Document. getnodes (node. element_node, node. comment_node, node. text_node );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.