JavaScript first Knowledge (a): Acquiring nodes

Source: Internet
Author: User

JAVASCRIOPT Dom has three main nodes: element node, attribute node, text node.

The three main methods of acquiring ELEMENT nodes are:

1.document.getelementbyid (); This method gets the node based on the unique ID value of the node.

such as <li id = "Hamigua" > Cantaloupe </li>,document.getelementbyid ("Hamigua");

2.document.getelementbytagname (); This method gets the node based on the label name of the node.

such as <li></li> tags, document.getelementbytagname ("Li");

3.document.getelementbyname (); This method gets the node based on the Name property of the node.

such as <input type = "text" name = "N_hamigua" value = "cantaloupe"/>,document.getelementbyname ("N_hamigua");

However, this method can only get a node with the name attribute, such as <li name = "Li_hamigua" ></li>,document.getelementbyname (Li_hamigua); You should pay attention to this problem when using.

Both the attribute node and the text node need to be fetched to the element node before acquiring it.

Attribute node: <input type = "text" id = "Input_hamihua" value = "Value_hamigua"/>;d Ocument.getelementbyid ("Input_hamigua") . GetAttributeNode ("value");

This allows you to get to the Value property of the input tag, which is the property of "Value_hamigua".

Text node: <li id = "Li_hamigua" > Cantaloupe </li>;d ocument.getelementbyid ("Li_hamigua"). FirstChild;

This allows you to get the text node of the input tag, which is the text value of "cantaloupe".

Explain the difference between the three nodes here:

① ELEMENT nodes: Similar to nodes such as <body></body>, <li></li>, <input/>.

② attribute node: Similar to <input type= "text" name= "text"/> "name= ' text" in such a node.

③ text node: a node similar to "HelloWorld" in <li>HelloWorld</li>.

JavaScript first Knowledge (a): Acquiring nodes

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.