06.js 4.3 KB

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