How JQuery parses HTML, JSON, and XML instances _ jquery

Source: Internet
Author: User
JQuery has powerful data parsing capabilities. This article describes how to parse the data of HTML, JSON, and XML files. 1. HTML

Sometimes, an HTML clip is saved in an HTML file. On the other homepage, the HTML file is read directly, and the HTML code snippet in the parsing is integrated into the home page.

Fragment.html file with the following content:

The Code is as follows:


Hello Jquery

On the homepage
Parse code in Test.html

The Code is as follows:


$ ("# A1"). click (function (){
$ ("# P2" pai.load('fragment.html ');
Return false;
}); [Code]

2. JSON
The JSON file is test. json with the following content:
[Code]
[{"Name": "jim", "age": "20" },{ "name": "lily", "age": "18", "holobby ": ["swim", "movie"]}]

On the homepage
Parse code in Test.html

The Code is as follows:


$ ("# A2"). click (function (){
$. GetJSON ('test. json', function (data ){
Var html ='










';$. Each (data, function (entryIndex, entry ){Html + =' ';If (entry. holobby ){Html + =' ';}Html + =' ';});Html + ='
'+ Entry. name +' '+ Entry. age +'';
$. Each (entry. holobby, function (lineindex, line ){
Html + = line + ",";
});
Html + ='
';
$ ("# P2" ).html (html );
Return false;
});
});


3. XML
The XML file is test. xml and its content is:

The Code is as follows:





Extjs
Zhang San
88


Sharp jQuery
Li Si
99


Introduction to flex
Wang Wu
108


Java programming ideas
Qian Qi
128



On the homepage
Parse code in Test.html

The Code is as follows:


$ ("# A3"). click (function (){
$. Get ('test. xml', function (data ){
Var s = "";
$ (Data). find ('book'). each (function (I ){
Var id = $ (this). attr ('id ');
Var name = $ (this). children ('name'). text ();
Var author = $ (this). children ('author'). text ();
Var price = $ (this). children ('price'). text ();
S + = id + "" + name + "" + author + "" + price +"
";
});
('{P2'{.html (s );
});
});


The original code of test.html is

The Code is as follows:






Related Article

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.