Getting started with AJAX --- getting started with HTML and AJAX --- ajax

Source: Internet
Author: User

Getting started with AJAX --- getting started with HTML and AJAX --- ajax
Getting started with AJAX --- DOM operation HTML

While learning about AJAX, I sat down and summed up my impression. What I wrote last time was how to use the XMLHttpRequest Object. Today I wrote DOM (Document Object Model) to operate on HTML, I hope my blog will be helpful to everyone's learning.

1. What is DOM?

Document Object Model (DOM) is a standard programming interface recommended by W3C for processing Extensible Markup Language. DOM can access and modify the content and structure of a document in a way independent of platform and language. In other words, this is a common way to represent and process an HTML or XML document.

2. simple HTML operations

Before implementing the example today, we need to understand common DOM nodes and common APIs. In this example, we dynamically add element nodes to HTML for the purpose of learning.

Final Effect

Add node implementation

// Add the node var index = 0; function appendnode () {// find the corresponding node var htmlNode = document.doc umentElement; var bodyNode = htmlNode. lastChild; // create a node var divNode = document. createElement ("div"); var textNode = document. createTextNode ("I am a new node" + index ++); // create a divNode relationship between nodes. appendChild (textNode); bodyNode. appendChild (divNode );}

Insert node implementation

// 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 new node" + index ++); newnode. appendChild (textnode); document.doc umentElement. childNodes [2]. insertBefore (newnode, firstdivnode); break;} firstdivnode = firstdivnode. nextSibling ;}}

Remove node implementation

Function removenode () {// 1. find the body // 2. find the div to be removed/3. call the remove method to remove the 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 ;}}

Summary

I always remember that a teacher always reminded me in the class that "good memory is not as good as bad writing." It is true that programming is not like this. It is worth watching the source code ten million times. For a complete demo, you can get it by downloading free source code (http://download.csdn.net/detail/senior_lee/7714311)


The Ajax jsp file has been written to connect to the database. How can the data be displayed on the HTML page?

This problem is too big. It is a bit abstract, that is, using javascript to control html elements. I don't know what elements, how to present them, and what the returned data looks like.

The ajaxahtml page returns the content in the specified div

Ajax can obtain the page return value as an independent html document operation and supports the jq selector operation. Therefore, you can obtain the source code in the copy block in the following ways:
Success: function (responseText ){
Var result{((responsetext;.find({copy}}.first().html ();
// Result is the source code in copy. Of course, you can use val () and text () to obtain relevant values.

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.