JavaScript manipulating DOM objects

Source: Internet
Author: User

I. Creating a Node

This piece of code creates a new <p> element

var dom = document.craeteelement ("P");

If you want <p> add text, you must create a node for the text

var node = document.createTextNode ("This is new Textnode");

Then you have to add this node to the <p> element

Dom.appendchild (node);

This time has been created successfully.

Two. Delete a node

For example, there is an HTML code

<div id= "Div1" ><p id= "P1" > This is a paragraph. </p><p id= "P2" > this is another paragraph. </p></div>

Script

<script>var Parent=document.getelementbyid ("Div1"); var Child=document.getelementbyid ("P1"); Parent.removechild (Child);</script>

JS Delete node When you need to find its parent node, if you can delete a node without referencing the parent element, it is very good, but unfortunately, the DOM needs to clear the element you want to delete, Yi and its parent element

This is a common practice:

var Child=document.getelementbyid ("P1"); Child.parentNode.removeChild (child);

  

JavaScript manipulating DOM objects

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.