Three methods for JavaScript to get nodes from html

Source: Internet
Author: User

Three methods for JavaScript to get nodes from html
How to get a node:
1. getElementById: Get the reference of the first object whose ID tag attribute is the specified value
2. getElementsByName obtains the object set based on the value of the NAME tag attribute.
3. getElementsByTagName obtains the object set based on the specified element name.

<Auxiliary> three attributes of each node: nodeName, nodeType, and nodeValue

NodeType: Label nodeoType is 1, attribute is 2, content is 3

 

The following shows three methods to obtain html nodes:

  Dom_1.html <Script type = text/javascript> 
 
// 1, getElementById function getNode1 () {var a = document. getElementById (divid); // obtain the Node object through the node id // alert (a); alert (. nodeName +, +. nodeType +, +. nodeValue); // The Name Of The div node // The nodeoType of the label is 1, the attribute is 2, and the content is 3 // nodeValue the label type node is null and has no value. Only attributes and text nodes can have values. // Obtain the content in the node only under the obtained node. // obtain the innerHTML innerText attribute var text = a in the div node. innerHTML; // set or obtain the HTML in the object's start and end tags. Alert (text); // change the attribute a. innerHTML = to something else. Note that innerHTML can modify the content. fontcolor (blue );}
// 2. getElementsByName obtains the object set based on the value of the NAME tag attribute. Function getNode2 () {var a = document. getElementsByName (aa); alert (. nodeName); // undefined a is an array. You must specify an element to use alert (a [0]. nodeName +-+ a [0]. nodeType +-+ a [0]. nodevalue );}
 
3,
Function getNode3 () {var a = document. getElementsByTagName (div); alert (. nodeName); // undefined a is an array. You must specify an element to use alert (a [0]. nodeName +-+ a [0]. nodeType +-+ a [0]. nodevalue) ;}</script>

This is the region where div is used. I am the best !! The effect is as follows:

 

 

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.