Introduction to Nodelist&namednodemap&htmlcollection in Dom __html

Source: Internet
Author: User
nodelist

Each node has a ChildNodes property that holds the NodeList object. NodeList is a class array object that holds an ordered set of nodes that can be accessed by location. The uniqueness of the NodeList object is that it is actually the result of dynamically executing the query based on the DOM structure, so changes in the DOM structure can be automatically reflected in the nodelist. NodeList is "dynamic":

var arrayofnodes = Array.prototype.slice.call (somenode.childnodes, 0);//Can be used to convert nodelist to a true array
NamedNodeMap

Similar to NodeList, it is also a collection of "dynamic". The attributes of an element are represented by a attr node, each of which is saved in the NamedNodeMap object. The NamedNodeMap object has the following methods.

name function
getNamedItem (name) Returns the node with the NodeName property equal to name
RemoveNamedItem (name) Remove the node with the NodeName property equal to name from the list
SetNamedItem (node) To add a node to the list, indexed by the NodeName property of the node
Item (POS) Returns the node at the location of the digital POS
Example
var id = element.attributes.getNamedItem (' id '). nodevalue;
or
var id = element.attributes["id"].nodevalue;
htmlcollection

A method commonly used to obtain an element reference is getElementsByTagName (). That is, to get the label name of the element, and return the nodelist containing 0 or more elements. In an HTML document, this method returns a Htmlcollection object as a "dynamic" collection that is very similar to nodelist.

Example: Gets all the  elements in the page and returns a htmlcollection.
var image = document.getElementsByTagName ("img");

Some methods

name function
Item () or square brackets Accessing items in a Htmlcollection object
Nameditem () Use this method to get the items in the collection through the element's name attribute
//e.g. just image
var myimage = Image.nameditem ("MyImage");
Returns the label of the first row

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.