Getting Started with Ajax---DOM manipulating html

Source: Internet
Author: User

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?
  1. Adding nodes
  2. var index = 0;
  3. function Appendnode () {
  4. Find body corresponding node
  5. var htmlnode = document.documentelement;
  6. var bodynode = Htmlnode.lastchild;
  7. New node
  8. var divnode = document.createelement ("div");
  9. var textnode = document.createTextNode ("I am a newly added node" +index++);
  10. Establish a relationship between nodes
  11. Divnode.appendchild (Textnode);
  12. Bodynode.appendchild (Divnode);
  13. }
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?
  1. Inserting nodes
  2. function Insertnode () {
  3. var removenode = document.getElementById ("Remove");
  4. var firstdivnode = removenode.nextsibling;
  5. while (Firstdivnode) {
  6. if (firstdivnode&& firstdivnode.nodename = = "DIV") {
  7. var newnode =document.createelement ("div");
  8. var textnode =document.createtextnode ("I am a newly added node" + index++);
  9. Newnode.appendchild (Textnode);
  10. Document.documentelement.childnodes[2].insertbefore (Newnode,firstdivnode);
  11. Break
  12. }
  13. Firstdivnode =firstdivnode.nextsibling;
  14. }
  15. }
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?
  1. function Removenode () {
  2. 1. Find Body
  3. 2. Find the div that needs to be removed
  4. 3. Call the Remove method to remove a node
  5. var bodynode = document.getElementById ("Remove"). parentnode;
  6. var removenode = document.getElementById ("Remove");
  7. var firstdivnode = removenode.nextsibling;
  8. while (Firstdivnode) {
  9. if (firstdivnode&& firstdivnode.nodename = = "DIV") {
  10. Bodynode.removechild (Firstdivnode);
  11. Break
  12. }
  13. Firstdivnode = firstdivnode.nextsibling;
  14. }
  15. }

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.