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)