浏览 1330 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2013-10-31
<html> <head> <title>Reading xml through jQuery</title> <style type="text/css"> h1{ cursor:pointer; font-size:20px; } ul { display:none; list-style:none; margin:opt; padding:opt; } </style> <script src="jqueryV1.10.2.js"></script> <script> $(document).ready(function(){ $('#result a').click(function(){ $('h1').live('click',function() { $(this).next('ul').toggle('fast'); }); $.ajax( { url: "common.xml", type: "GET", dataType: "xml", success: function(xml){ var str=''; $(xml).find('book').each(function(){ var book=$(this); str+='<h1>'+book.find('name').text()+'</h1>'; str+='<ul>'; book.find('story').each(function(){ str+='<li>'; str+=$(this).find('title').text(); str+='</li>'; }); str+='</ul>'; }); $('#result').html(str); } }); }); }); </script> </head> <body> <div id="result"> <a href="#">Click here to get retails.</a> </div> </body> </html> 相应的xml代码如下: <?xml version="1.0" encoding="utf-8"?> <books><book index="1"><name year="1892">The Adventures of Sherlock Holmes</name><story><title>A Scandal in Bohemia</title><quote>You see,but you don't observe.The distinction is clear.</quote></story><story><title>The Red-headed League</title><quote>It is quite a three pipe problem, and I beg that you won't speak to me for fifty minutes.</quote></story><story><title>The Man with the Twisted Lip</title><quote>It is, of course, a trifle, but there is nothing so important as trifles.</quote></story><story><title>New story</title><quote>New quote</quote></story></book> <book index="2"><name year="1927">The Case-book of Sherlock Holmes</name><story><title>The Adventure of the Three Gables</title><quote>I am not the law, but I represent justice so far as my feeble powers go.</quote></story><story><title>The Adventure of Shoscombe Old Place</title><quote>Dogs don't make mistakes.</quote></story><story><title/><quote/></story></book> <book index="3"><name year="1893">The Memories of Sherlock Holmes</name><story><title>The Yellow Face.</title><quote>Any truth is better than indefinite doubt.</quote></story><story><title>The Stockbroker's Clerk.</title><quote>Results without causes are much more impressive.</quote></story><story><title>The Final Problem.</title>\ <quote>If I were assured of your eventual destuction I would, in the interests of the public, cheerfully accept my own.</quote></story></book> </books> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |