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