The article provides two examples of jquery webpage reading special effects on files for free. These codes are very practical. They are the second complete jquery code for reading json files.
Json file:
[
{
"Name": "xujun ",
"Sex": "male ",
"Home": "nanjing"
},
{
"Name": "jack ",
"Sex": "male ",
"Home": "beijing"
}
]
Html file:
<! Doctype html public "-// w3c // dtd html 4.01 transitional // en" "http: // wwwbKjia. c0m/tr/html4/loose. dtd">
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
<Script src = ".../jquery. js"> </script>
<Script>
$ (Document). ready (function (){
$. Getjson ('jsondata. json? Id', function (data ){
// Traverse every entry in json
$. Each (data, function (entryindex, entry ){
Var html = '<tr> ';
Html + = '<td>' + entry ['name'] + '</td> ';
Html + = '<td>' + entry ['sex'] + '</td> ';
Html + = '<td>' + entry ['home'] + '</td> ';
Html + = '</tr> ';
$ ('# Title'). after (html );
});
});
});
</Script>
</Head>
<Body>
<Table>
<Tr id = "title">
<Th> name </th>
<Th> gender </th>
<Th> Home address </th>
</Tr>
</Table>
</Body>
</Html>
Instance 2
<! Doctype html public "-// w3c // dtd html 4.01 // en" "http://www.w3.org/tr/html4/strict.dtd">
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = iso-8859-1">
<Title> json document </title>
<Script type = "text/Webpage effects" src = "lib/jquery/jquery-1.3.2.js"> </script>
<Script type = "text/javascript">
$ (Function (){
$ ("# Button"). click (function (){
$. Getjson (B. json, function (data) {// The getjson method obtains the code of the json file. The callback function parameter data is the json code.
$ ("# Json"). empty (); // clear the content of the div tag with "json" id
$. Each (data, function (entryindex, entry) {// The each method traverses objects in the json file. entryindex is the current index and entry is the current item.
Var html = "<div class = entry> ";
Html + = "
Html + = "<div class = part>" + entry [part] + "</div> ";
Html + = "<div class = definition>" + entry [definition];
If (entry [quote]) {
Html + = "<div class = quote> ";
$. Each (entry [quote], function (quoteindex, line ){
Html + = "<p>" + line + "</p> ";
});
Html + = "</div> ";
}
Html + = "</div> ";
Html + = "</div> ";
$ ("# Json"). append (html );
});
});
});
});
</Script>
</Head>
<Body>
<Div id = "button"> <B> get json document </B> </div>
<Div id = "json"> </div>
</Body>
</Html>
1 2