htmldom-Delete an existing HTML element

Source: Internet
Author: User

Excerpted from www.w3school.com:http://www.w3school.com.cn/htmldom/dom_elements.asp
To delete an HTML element, you must be aware of the element's parent 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>

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

<DivID= "Div1"><PID= "P1">This is a paragraph.</P><PID= "P2">This is another paragraph.</P></Div>

Find elements of 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: Can I delete an element without referencing the parent element?

I'm so sorry. The DOM needs to understand the elements you need to delete, as well as its parent elements.

Here's a common workaround: Find the child element you want to delete, and use the ParentNode property to find its parent element:

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

htmldom-Delete an existing 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.