JavaScript getting elements, setting properties

Source: Internet
Author: User
Tags tag name

There are 3 DOM methods to get ELEMENT nodes, which are obtained by ID, tag name, class name, respectively.

1.getElementsById

Access the element by ID, note that the initial letter is not written in uppercase, and if it is written incorrectly as "getElementById", it is invalid.

Document.getelementsbyid (ID) This ID value must be written in single or double quotes

2.getElementsByTagName

getElementsByTagName accesses an element by its tag name, the method returns an array of objects, each corresponding to an element in the document that has a given label.

document.getElementsByTagName (TAG)

3.getElementsByClassName

Getelementsbyclassname accesses elements through the class name, similar to TagName, which returns an array, and it can also find elements with multiple class names.

Getelementsbyclassname (Class)

Note that access to an element with multiple class names is not possible without access to multiple class names.

1 document.getelementsbyclassname ("a B"); 2 // can only access 3 <div class= "a B" ></div>4// No access 5 <div class= "a" > </div>6 <div class= "B" ></div>

Get and Set properties

1.getAttribute

This method is used to get the individual property values of the node, but not the document object, so it cannot be called through the Document object.

1 //usage2<p title= "Hello" >Yes!</p>3<p>Yes!</p>4 varparas = document.getelementsbytagname ("P");5  for(vari = 0; i < paras.length; i++ ){6Title_text = paras[I].getattribute ("title");7     if(Title_text! =NULL) {//equivalent (Title_text)8 alert (title_text);9     } Ten}

2.setAttribute

SetAttribute () is used to set the individual property values of the node.

1 //usage2<p title= "Hello" >Yes!</p>3 varparas = document.getelementsbytagname ("P");4  for(vari = 0; i < paras.length; i++ ){5     varTitle_text = paras[I].getattribute ("title");6     if(title_text) {7paras[I].setattribute ("title", "Hey");8Alert (paras[i].getattribute ("title") );9     }Ten}

These 5 methods are the basis for writing many DOM scripts!

JavaScript getting elements, setting properties

Related Article

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.