Web Development Technology--javascript HTML DOM3 (Element)

Source: Internet
Author: User

Add and remove nodes (HTML elements). Create a new HTML element

To add a new element to the HTML DOM, you must first create the element (the element node), and then append the element to an existing element.

<DivID= "Div1">    <PID= "P1">This is a paragraph</P>    <PID= "P2">This is another paragraph</P></Div> <Script>    varpara=Document.createelement ("P"); varnode=document.createTextNode ("This is a new paragraph. ");     Para.appendchild (node); varelement=document.getElementById ("Div1"); Element.appendchild (para);</Script>

Example Explanation:

This code creates a new <p> element:

var para=document.createelement ("P");

To add text to the <p> element, you must first create a text node. This piece of code creates a text node:

var Node=document.createtextnode ("This is the new paragraph. ");

Then you must append the text node to the <p> element:

Para.appendchild (node);

Finally, you must append this new element to an existing element.

This code finds an existing element:

var Element=document.getelementbyid ("Div1");

This code appends a new element to the existing element:

Element.appendchild (para);
Delete an existing HTML element

To delete an HTML element, you must first obtain the parent element of the element:

<DivID= "Div1">    <PID= "P1">This is a paragraph.</P>    <PID= "P2">This is another paragraph.</P></Div> <Script>    varParent=document.getElementById ("Div1"); var Child=document.getElementById ("P1");p arent.removechild (child);</Script>

Example Explanation:

This HTML document contains <div> elements with two child nodes (two <p> elements):

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

Find the element that id= "DIV1":

var Parent=document.getelementbyid ("Div1");

Find the <p> elements of Id= "P1":

var Child=document.getelementbyid ("P1");

To remove a child element from the parent element:

Parent.removechild (child);

Tip: It's great to be able to delete an element without referencing the parent element.

But I'm sorry. The DOM needs to be clear about the element you need to delete, and its parent element.

This is a common solution: find the child element that you want to delete, and then use its ParentNode property to find the parent element:

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

In the HTML DOM section of our JavaScript tutorial, you've learned:

· How to change the contents of an HTML element (InnerHTML)

· How to change the style of HTML elements (CSS)

· How to respond to HTML DOM events

· How to add or remove HTML elements

Web Development Technology--javascript HTML DOM3 (Element)

Related Article

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.