Javascript:dom node (new HTML element/delete HTML element)

Source: Internet
Author: User

You can create new HTML elements using the DOM, or you can delete existing HTML elements.

code collated from W3school:http://www.w3school.com.cn

(i) New elements:

<script>    //Create a new <p> element    var newele=document.createelement ("P");    Create a text node    var node=document.createtextnode ("This is a new paragraph created with JavaScript. ");    Add a text node to the newly created <p> element    newele.appendchild (node);    var Div1_ele=document.getelementbyid ("Div1");    Adds the newly created element to the existing element    Div1_ele.appendchild (newele);  </script>
(ii) Delete HTML elements

<script>      //Delete an element, you must first get to its parent element      function Deleteele () {        var parent = document.getElementById ("Div_ ");        var child = document.getElementById ("Div_02_p2");        Parent.removechild (child);      }      Gets the parent element of the element using code      function DeleteEle3 () {        var child = document.getElementById ("Div_02_p3");        Child.parentNode.removeChild (child);      }    </script>


Example code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">



Javascript:dom node (new HTML element/delete HTML element)

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.