06.js 4.9 KB

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