Home > Article > Web Front-end > How to use js Element Traversal to standardize element traversal
This time I will show you how to use js Element Traversal to standardize element traversal, and what are the precautions for using js Element Traversal to standardize element traversal. The following is a practical case, let's take a look.
For spaces between elements, document nodes will not be returned before IE9.All other browsers will return document nodes.
In order to be compatible with the differences between browsers without changing the existing DOM standard, the Element Traversal specification was created. This specification adds 5 childElementCountfirstElementChild
lastElementChild
previousElementSibling
nextElementSibling
comments) number.
var i,len,child = element.firstChild; while(child != element.lastChild){ if(child.nodeType == 1){ processChild(child); } child = child.nextSibling; }When using Element Traversal
to add attributes, the code will change It is very concise:
var i,len,child = element.firstElementChild; while(child != element.lastElementChild){ processChild(child); child = child.nextElementSibling; }I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
How to use bootstrap in vue-cli project
How to operate Node.Js to generate Bitcoin address
The above is the detailed content of How to use js Element Traversal to standardize element traversal. For more information, please follow other related articles on the PHP Chinese website!