Home > Article > Web Front-end > Usage examples of combining jQuery and getJson
This article mainly introduces the usage of jQuery and getJson. It analyzes the related operation skills of jquery to parse json data and array traversal with examples. It has certain reference value. Friends who need it can refer to it. The details are as follows:
Here is an application example of the combination of jQuery and getJson. Because json is relatively lightweight and practical to save data, I personally prefer the Json function. I used this to simply write a navigation menu function. It is relatively simple and the purpose is to demonstrate how to use it. Combine jQuery with Json.
The screenshot of the running effect is as follows:
The specific code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jQuery与Json结合的一个应用例子</title> <script src="jquery-1.6.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ var data = { "siteData" : [ { "siteClass" : "网页制作", "siteList" : [ {"sName" : "脚本之家", "url" : "//www.jb51.net/"}, {"sName" : "51CTO", "url" : "http://www.51cto.com/"}, {"sName" : "博客园", "url" : "http://www.cnblogs.com/"} ] }, { "siteClass" : "在线音乐", "siteList" : [ {"sName" : "百度MP3", "url" : "http://mp3.baidu.com/"}, {"sName" : "千千静听", "url" : "http://www.music2.com/"}, {"sName" : "酷狗音乐", "url" : "http://www.kugou.com/"} ] }, { "siteClass" : "求职招聘", "siteList" : [ {"sName" : "58同城", "url" : "http://www.58.com/"}, {"sName" : "赶集网", "url" : "http://www.ganji.com/"} ] } ] } var items = []; $.each(data.siteData, function(i, val) { var li2Size = val.siteList.length; for(var m=0, li2 = ''; m < li2Size; m++){ li2 += '<li id="li_'+i+'_'+m+'"><a href="' + val.siteList[m].url + '" title="' + val.siteList[m].sName + '" target="_blank">' + val.siteList[m].sName + '</a></li>'; } items.push('<li><dl id="li_' + i + '"><dt>' + val.siteClass +'</dt><dd><ul>'+ li2 + '</ul></dd></dl></li>'); }); $('<ul/>', { 'style': 'color:red;', 'class': 'my-new-list', html: items.join('') }).appendTo('body'); }) </script> </head> <body> </body> </html>
The above is the entire content of this chapter, more related tutorials Please visit jQuery Video Tutorial, JSON Video Tutorial!