Common DOM operations in js

Source: Internet
Author: User
& Lt; html & gt; & lt; head & gt; & lt;/head & gt; & lt; body & gt; & lt; formid & quot; myform & quot; & gt; & lt; inputtype & quot; text & quot; value & quot; get id & quot; getId & quot; & gt; & lt; in
Script // DOM object method // getElementById returns the element with the specified ID/* var byid = document. getElementById ("getId"); alert (byid. value); // get id // getElementsByTagName returns the list of nodes (Set/node array) containing all elements with the specified tag name var tagname = document. getElementsByTagName ("input"); alert (tagname [0]. value); // get id // createElement create Element Node var myform = document. getElementById ("myform"); var e = document. createElement ("input"); // create the input element e. type = "button"; // defines the input type value e. value = "haha"; // defines the value for the input value // appendChild () adds the new subnode to the specified node myform. appendChild (e); // Add the created input form to the form // insertBefore () Insert a new subnode document before the specified subnode. body. insertBefore (e, myform); // Add input to front of form // createAttribute () Create attribute node var att = document. createAttribute ("class"); att. value = "democlass"; // setAttributeNode () method to add a new property node document. getElementsByTagName ("input") [0]. setAttributeNode (att); // createElement creates an element node var newel = document. createElement ("p"); // The createTextNode () method creates a new text node newtext = document. createTextNode ('xixihahaha'); // appendChild () adds a new subnode to the specified node newel. appendChild (newtext); // appendChild () adds a new subnode to the specified node myform. appendChild (newel); // setAttribute () can be used to create a new property when the property does not exist. We can use this method to create a new property x = document. getElementsByTagName ("input"); x [0]. setAttribute ("asdasd", "first"); // replaceChild () method: Replace the node // The first parameter is the new node // The second parameter is the old node (the node to be replaced) var y1 = document. getElementsByTagName ("input") [1]; var y2 = document. getElementsByTagName ("input") [2]; myform. replaceChild (y2, y1); // removeChild () method to delete a specified node // when the node to be deleted is located, you can use the parentNode attribute and removeChild () method to delete this node var span1 = document. getElementsByTagName ("span") [0]; span1.parentNode. removeChild (span1); */script

 

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.