While learning Ajax while sitting to summarize the deepening impression, the last time I wrote is how to use the XMLHttpRequest object, today is written in the DOM (Document Object model) operation HTML, I hope my blog will help everyone's study. A What is DOM
The file object model, or DOM, is the standard programming interface recommended by the Organization for the processing of extensible flag languages. The DOM can access and modify the content and structure of a document in a platform-independent and language way. In other words, this is a common way of representing and working with an HTML or XML document.
Two Simple manipulation of HTML
Today's example before we need to understand the DOM's common nodes and common APIs, examples of the dynamic HTML to add element nodes to achieve the purpose of learning.
Final effect
Adding a node implementation
[Plain]View Plaincopyprint?
- Adding nodes
- var index = 0;
- function Appendnode () {
- Find body corresponding node
- var htmlnode = document.documentelement;
- var bodynode = Htmlnode.lastchild;
- New node
- var divnode = document.createelement ("div");
- var textnode = document.createTextNode ("I am a newly added node" +index++);
- Establish a relationship between nodes
- Divnode.appendchild (Textnode);
- Bodynode.appendchild (Divnode);
- }
Add node var index = 0;function Appendnode () { //Find body corresponding node var htmlnode = document.documentelement; var bodynode = Htmlnode.lastchild; New node var divnode = document.createelement ("div"); var textnode = document.createTextNode ("I am a newly added node" +index++); Establish the relationship between nodes Divnode.appendchild (textnode); Bodynode.appendchild (Divnode);}
Insert Node Implementation
[Plain]View Plaincopyprint?
- Inserting nodes
- function Insertnode () {
- var removenode = document.getElementById ("Remove");
- var firstdivnode = removenode.nextsibling;
- while (Firstdivnode) {
- if (firstdivnode&& firstdivnode.nodename = = "DIV") {
- var newnode =document.createelement ("div");
- var textnode =document.createtextnode ("I am a newly added node" + index++);
- Newnode.appendchild (Textnode);
- Document.documentelement.childnodes[2].insertbefore (Newnode,firstdivnode);
- Break
- }
- Firstdivnode =firstdivnode.nextsibling;
- }
- }
Insert Node function Insertnode () { var removenode = document.getElementById ("Remove"); var firstdivnode = removenode.nextsibling; while (Firstdivnode) { if (firstdivnode&& firstdivnode.nodename = = "DIV") { var newnode = Document.createelement ("div"); var textnode =document.createtextnode ("I am a newly added node" + index++); Newnode.appendchild (textnode); Document.documentelement.childnodes[2].insertbefore (Newnode,firstdivnode); break; } Firstdivnode =firstdivnode.nextsibling; }}
Removing node implementations
[Plain]View Plaincopyprint?
- function Removenode () {
- 1. Find Body
- 2. Find the div that needs to be removed
- 3. Call the Remove method to remove a node
- var bodynode = document.getElementById ("Remove"). parentnode;
- var removenode = document.getElementById ("Remove");
- var firstdivnode = removenode.nextsibling;
- while (Firstdivnode) {
- if (firstdivnode&& firstdivnode.nodename = = "DIV") {
- Bodynode.removechild (Firstdivnode);
- Break
- }
- Firstdivnode = firstdivnode.nextsibling;
- }
- }