6.21.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. $(document).ready(function() {
  2. $('#letter-a a').click(function(event) {
  3. event.preventDefault();
  4. $.ajax({
  5. url: 'a.html',
  6. success: function(data) {
  7. $('#dictionary').html(data);
  8. }
  9. });
  10. });
  11. $('#letter-b a').click(function(event) {
  12. event.preventDefault();
  13. $.getJSON('b.json', function(data) {
  14. var html = '';
  15. $.each(data, function(entryIndex, entry) {
  16. html += '<div class="entry">';
  17. html += '<h3 class="term">' + entry.term + '</h3>';
  18. html += '<div class="part">' + entry.part + '</div>';
  19. html += '<div class="definition">';
  20. html += entry.definition;
  21. if (entry.quote) {
  22. html += '<div class="quote">';
  23. $.each(entry.quote, function(lineIndex, line) {
  24. html += '<div class="quote-line">' + line + '</div>';
  25. });
  26. if (entry.author) {
  27. html += '<div class="quote-author">' + entry.author + '</div>';
  28. }
  29. html += '</div>';
  30. }
  31. html += '</div>';
  32. html += '</div>';
  33. });
  34. $('#dictionary').html(html);
  35. });
  36. });
  37. $('#letter-c a').click(function(event) {
  38. event.preventDefault();
  39. $.getScript('c.js');
  40. });
  41. $('#letter-d a').click(function(event) {
  42. event.preventDefault();
  43. $.get('d.xml', function(data) {
  44. $('#dictionary').empty();
  45. $(data).find('entry').each(function() {
  46. var $entry = $(this);
  47. var html = '<div class="entry">';
  48. html += '<h3 class="term">' + $entry.attr('term');
  49. html += '</h3>';
  50. html += '<div class="part">' + $entry.attr('part');
  51. html += '</div>';
  52. html += '<div class="definition">';
  53. html += $entry.find('definition').text();
  54. var $quote = $entry.find('quote');
  55. if ($quote.length) {
  56. html += '<div class="quote">';
  57. $quote.find('line').each(function() {
  58. html += '<div class="quote-line">';
  59. html += $(this).text() + '</div>';
  60. });
  61. if ($quote.attr('author')) {
  62. html += '<div class="quote-author">';
  63. html += $quote.attr('author') + '</div>';
  64. }
  65. html += '</div>';
  66. }
  67. html += '</div>';
  68. html += '</div>';
  69. $('#dictionary').append($(html));
  70. });
  71. });
  72. });
  73. $('#letter-e a').click(function(event) {
  74. event.preventDefault();
  75. var requestData = {term: $(this).text()};
  76. $.get('e.php', requestData, function(data) {
  77. $('#dictionary').html(data);
  78. }).fail(function(jqXHR) {
  79. $('#dictionary')
  80. .html('Sorry, but an error occurred: ' + jqXHR.status)
  81. .append(jqXHR.responseText);
  82. });
  83. });
  84. $('#letter-f form').submit(function(event) {
  85. event.preventDefault();
  86. var formValues = $(this).serialize();
  87. $.get('f.php', formValues, function(data) {
  88. $('#dictionary').html(data);
  89. });
  90. });
  91. var url = 'http://examples.learningjquery.com/jsonp/g.php';
  92. $('#letter-g a').click(function(event) {
  93. event.preventDefault();
  94. $.getJSON(url + '?callback=?', function(data) {
  95. var html = '';
  96. $.each(data, function(entryIndex, entry) {
  97. html += '<div class="entry">';
  98. html += '<h3 class="term">' + entry.term + '</h3>';
  99. html += '<div class="part">' + entry.part + '</div>';
  100. html += '<div class="definition">';
  101. html += entry.definition;
  102. if (entry.quote) {
  103. html += '<div class="quote">';
  104. $.each(entry.quote, function(lineIndex, line) {
  105. html += '<div class="quote-line">' + line + '</div>';
  106. });
  107. if (entry.author) {
  108. html += '<div class="quote-author">' + entry.author + '</div>';
  109. }
  110. html += '</div>';
  111. }
  112. html += '</div>';
  113. html += '</div>';
  114. });
  115. $('#dictionary').html(html);
  116. });
  117. });
  118. var $loading = $('<div id="loading">Loading...</div>')
  119. .insertBefore('#dictionary');
  120. $(document).ajaxStart(function() {
  121. $loading.show();
  122. }).ajaxStop(function() {
  123. $loading.hide();
  124. });
  125. $('body').on('click', 'h3.term', function() {
  126. $(this).siblings('.definition').slideToggle();
  127. });
  128. });