Basic JavaScript DOM operation example

Source: Internet
Author: User

Document Object Model (DOM) is an interface for intercommunication between website content and JavaScript. When JavaScript becomes the most common language, JavaScript and DOM are usually considered as independent entities. The DOM interface is used to access, traverse, and control HTML and XML documents. Below we will briefly introduce some basic JavaScript DOM operation instances, including creating, adding, deleting, cloning, and accessing nodes.

BKJIA recommended reading: The nature and operation methods of JavaScript DOM

1. getElementById (id)

This is one of the most commonly used methods to access an element through id. For example:

 
 
  1.          <body> 
  2.          <div id="myid"> 
  3.          test  
  4.          </div> 
  5.          <script language="javascript"> 
  6.          alert(document.getElementById("myid").innerHTML);  
  7.          </script> 
  8.          </body> 

Note: If the element ID is not unique, the first element with the ID name is obtained.

2. getElementsByName (name)

This is to get a bunch of elements as an array through name. You can see that there is a small s behind the Element. The ID is required to be unique in the HTML document, and the name may not be unique, for example, in checkbox and radio, multiple inputs are used with the same name to identify whether they are in the same party. By the way, getElementsByName (name) is only used to obtain input, radio, checkbox, and other elements, such as <input name = "myradio" type = "radio"/>.

3. getElementsByTagName (tagname)

By reading this method, we can see that this is also to get a bunch of elements as an array), which is obtained through TagName, that is, the tag name. You can traverse this array to obtain each individual element. When a DOM structure is large, it can be used to effectively narrow the search scope.

 
 
  1. <Html>
  2. <Head>
  3. <Script>
  4. Function test (){
  5. Testall = document. getElementsByTagName ("body ");
  6. Testbody = testall. item (0); // obtain all the elements whose tagnames are body. Of course, each page has only one element)
  7. Testall = testbody. getElementsByTagName ("p"); // obtain all P elements of the body sub-element
  8. Testnode = testall. item (1); // obtain the second P element.
  9. Alert (testnode. firstChild. nodeValue); // display the text of this element}
  10. </Script>
  11. </Head>
  12. <Body>
  13. <P> hi </p>
  14. <P> hello </p>
  15. <Script language = "javascript">
  16. Test ();
  17. </Script>
  18. </Body>
  19. </Html>

4. appendChild (node)

Append a node to the current element.

 
 
  1.          <body> 
  2.          
  3.          
  4.          <div id="test"></div> 
  5.          <script type="text/javascript"> 
  6.          var newdiv=document.createElement("div")  
  7.          var newtext=document.createTextNode("A new div")           
  8.          newdiv.appendChild(newtext)  
  9.          document.getElementById("test").appendChild(newdiv)  
  10.          </script> 
  11.          </body> 

I used innerHTML to display the content in the first example. I saw the article just now and learned that innerHTMl does not belong to DOM.

5. removeChild (childreference)

Delete the subnode of the current node and return the deleted node. The deleted node can be inserted somewhere else.

 
 
  1.          <body> 
  2.          <div id="parent"><div id="child">A child</div></div> 
  3.          <script language="javascript"> 
  4.          var childnode=document.getElementById("child")  
  5.          var removednode=document.getElementById("parent").removeChild(childnode)  
  6.          </script> 
  7.          </body> 

6. cloneNode (deepBoolean)

Copy and return the copy node of the current node. The copy node is an isolated node. It copies the attributes of the original node. Before adding the new node to the document, modify the ID attribute as needed to ensure that the ID is unique. This method supports a Boolean parameter. When deepBoolean is set to true, all subnodes of the current node are copied, including the text in the node.

 
 
  1.          <body> 
  2.          <p id="mynode">test</p> 
  3.          <script language="javascript"> 
  4.          p=document.getElementById("mynode")   
  5.         ppclone = p.cloneNode(true);  
  6.          p.parentNode.appendChild(pclone);  
  7.          </script> 
  8.          </body> 

7. replaceChild (newChild, oldChild)

Replace one subnode of the current node with another node.

 
 
  1.          <body> 
  2.          <div id="mynode2"> 
  3.          <span id="orispan">span</span> 
  4.          </div> 
  5.          <script language="javascript"> 
  6.          var orinode=document.getElementById("orispan");  
  7.          var newnode=document.createElement("p");  
  8.          var text=document.createTextNode("test ppp ");  
  9.          newnode.appendChild(text);  
  10.          document.getElementById("mynode2").replaceChild(newnode, orinode);  
  11.          </script> 
  12.          </body> 

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.