06.js 5.1 KB

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