06.js 5.4 KB

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