jquery学习笔记(5) AJAX
1. 添加HTML $('#dictionary').load('a.html') 2. 处理JSON数据 $.getJSON() JSON数据为: [ { "term": "BACCHUS", "part": "n.", "definition": "A convenient deity invented by the ancients as an excuse for getting drunk.", "quote": [ "Is public worship, then, a sin,", "That for devotions paid to Bacchus", "The lictors dare to run us in,", "And resolutely thump and whack us?" ], "author": "Jorace" }, { "term": "BACKBITE", "part": "v.t.", "definition": "To speak of a man as you find him when he can't find you." }, { "term": "BEARD", "part": "n.", "definition": "The hair that is commonly cut off by those who justly execrate the absurd Chinese custom of shaving the head." } ] 处理方法如下: $.getJSON('b.json', function(data) { $('#dictionary').empty(); $.each(data, function(entryIndex, entry) { var html = '
'; html += '
' + entry['term'] + '
'; html += '
' + entry['part'] + '
'; html += '
'; html += entry['definition']; if (entry['quote']) { html += '
'; $.each(entry['quote'], function(lineIndex, line) { html += '
' + line + '
'; }); if (entry['author']) { html += '
' + entry['author'] + '
'; } html += '
'; } html += '
'; html += '
'; $('#dictionary').append(html); }); }); 3. 执行脚本 $.getScript() 4. 处理XML数据 $.get('d.xml', function(data) { $(data).find('entry').each(function() { ... }); }); 5. 与服务器交互 get(url, data, callback, type) post(url, data, callback, type) 6. 组织数据 serialize() $(this).find('input').serialize() 7. AJAX事件 ajaxStart( ) ajaxStop( ) ajaxSend( ) ajaxComplete( ) ...