// Example A: Loading HTML files // Step 1 // $(document).ready(function() { // $('#letter-a a').click(function() { // $('#dictionary').load('a.html'); // return false; // }); // }); // Step 2 $(document).ready(function() { $('#letter-a a').click(function() { $('#dictionary').hide().load('a.html', function() { $(this).fadeIn(); }); return false; }); }); // Example B: Loading JSON files // Step 1 // $(document).ready(function() { // $('#letter-b a').click(function() { // $.getJSON('b.json'); // return false; // }); // }); // Step 2 // $(document).ready(function() { // $('#letter-b a').click(function() { // $.getJSON('b.json', function(data) { // $('#dictionary').empty(); // $.each(data, function(entryIndex, entry) { // var html = '
'; // html += '

' + entry['term'] + '

'; // html += '
' + entry['part'] + '
'; // html += '
'; // html += entry['definition']; // html += '
'; // html += '
'; // $('#dictionary').append(html); // }); // }); // return false; // }); // }); // Step 3 $(document).ready(function() { $('#letter-b a').click(function() { $.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); }); }); return false; }); }); // Example C: Loading JavaScript files $(document).ready(function() { $('#letter-c a').click(function() { $.getScript('c.js'); return false; }); }); // Example D: Loading XML files // Step 1 // $(document).ready(function() { // $('#letter-d a').click(function() { // $.get('d.xml', function(data) { // // }); // return false; // }); // }); // Step 2 // $(document).ready(function() { // $('#letter-d a').click(function() { // $.get('d.xml', function(data) { // $('#dictionary').empty(); // $(data).find('entry').each(function() { // var $entry = $(this); // var html = '
'; // html += '

' + $entry.attr('term') // + '

'; // html += '
' + $entry.attr('part') // + '
'; // html += '
'; // html += $entry.find('definition').text(); // var $quote = $entry.find('quote'); // if ($quote.length) { // html += '
'; // $quote.find('line').each(function() { // html += '
' // + $(this).text() + '
'; // }); // if ($quote.attr('author')) { // html += '
' // + $quote.attr('author') + '
'; // } // html += '
'; // } // html += '
'; // html += '
'; // $('#dictionary').append($(html)); // }); // }); // return false; // }); // }); // Step 3 $(document).ready(function() { $('#letter-d a').click(function() { $.get('d.xml', function(data) { $('#dictionary').empty(); $(data).find('entry:has(quote[author])').each(function() { var $entry = $(this); var html = '
'; html += '

' + $entry.attr('term') + '

'; html += '
' + $entry.attr('part') + '
'; html += '
'; html += $entry.find('definition').text(); var $quote = $entry.find('quote'); if ($quote.length) { html += '
'; $quote.find('line').each(function() { html += '
' + $(this).text() + '
'; }); if ($quote.attr('author')) { html += '
' + $quote.attr('author') + '
'; } html += '
'; } html += '
'; html += '
'; $('#dictionary').append($(html)); }); }); return false; }); }); // Example E: Sending data to the server // Step 1 // $(document).ready(function() { // $('#letter-e a').click(function() { // $.get('e.php', {'term': $(this).text()}, function(data) { // $('#dictionary').html(data); // }); // return false; // }); // }); // Step 2 // $(document).ready(function() { // $('#letter-e a').click(function() { // $.post('e.php', {'term': $(this).text()}, function(data) { // $('#dictionary').html(data); // }); // return false; // }); // }); // Step 3 $(document).ready(function() { $('#letter-e a').click(function() { $('#dictionary').load('e.php', {'term': $(this).text()}); return false; }); }); // Example F: Submitting forms // Step 1 // $(document).ready(function() { // $('#letter-f form').submit(function() { // $('#dictionary').load('f.php', // {'term': $('input[name="term"]').val()}); // return false; // }); // }); // Step 2 $(document).ready(function() { $('#letter-f form').submit(function() { $.get('f.php', $(this).serialize(), function(data) { $('#dictionary').html(data); }); return false; }); }); // Example: Monitoring the request $(document).ready(function() { $('
Loading...
') .insertBefore('#dictionary') .ajaxStart(function() { $(this).show(); }).ajaxStop(function() { $(this).hide(); }); }); // Example: AJAX and Events // Step 1 // $(document).ready(function() { // $('.term').click(function() { // $(this).siblings('.definition').slideToggle(); // }); // }); // Step 2 $(document).ready(function() { $('.term').live('click', function() { $(this).siblings('.definition').slideToggle(); }); }); // Example G: JSONP $(document).ready(function() { var url = 'http://examples.learningjquery.com/jsonp/g.php'; $('#letter-g a').click(function() { $.getJSON(url + '?callback=?', 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); }); }); return false; }); }); // Example H: Loading of page fragmemts // Step 1 // $(document).ready(function() { // $('#letter-h a').click(function() { // $('#dictionary').load('h.html'); // return false; // }); // }); // Step 2 $(document).ready(function() { $('#letter-h a').click(function() { $('#dictionary').load('h.html .entry'); return false; }); });