1.

  1. /******************************************************************************
  2. Our plugin code comes first in this document. Normally, plugins would
  3. appear in separate files named jquery.plugin-name.js, but for our examples
  4. it's convenient to place this plugin code in the same JavaScript file as
  5. the code that calls it.
  6. ******************************************************************************/
  7.  
  8. /******************************************************************************
  9. $.sum()
  10. Return the total of the numeric values in an array/object.
  11. ******************************************************************************/
  12. (function($) {
  13. $.mathUtils = {
  14. sum: function(array) {
  15. var total = 0;
  16.  
  17. $.each(array, function(index, value) {
  18. value = $.trim(value);
  19. value = parseFloat(value) || 0;
  20.  
  21. total += value;
  22. });
  23. return total;
  24. },
  25. average: function(array) {
  26. if ($.isArray(array)) {
  27. return $.mathUtils.sum(array) / array.length;
  28. }
  29. return '';
  30. }
  31. };
  32. })(jQuery);
  33.  
  34. /******************************************************************************
  35. .swapClass()
  36. Exchange one class for another on the selected elements.
  37. ******************************************************************************/
  38. (function($) {
  39. $.fn.swapClass = function(class1, class2) {
  40. return this.each(function() {
  41. var $element = $(this);
  42. if ($element.hasClass(class1)) {
  43. $element.removeClass(class1).addClass(class2);
  44. }
  45. else if ($element.hasClass(class2)) {
  46. $element.removeClass(class2).addClass(class1);
  47. }
  48. });
  49. };
  50. })(jQuery);
  51.  
  52. /******************************************************************************
  53. .shadow()
  54. Create a shadow effect on any element by brute-force copying.
  55. ******************************************************************************/
  56. (function($) {
  57. $.fn.shadow = function(opts) {
  58. var options = $.extend({}, $.fn.shadow.defaults, opts);
  59.  
  60. return this.each(function() {
  61. var $originalElement = $(this);
  62. for (var i = 0; i < options.copies; i++) {
  63. var offset = options.copyOffset(i);
  64. $originalElement
  65. .clone()
  66. .css({
  67. position: 'absolute',
  68. left: $originalElement.offset().left + offset.x,
  69. top: $originalElement.offset().top + offset.y,
  70. margin: 0,
  71. zIndex: -1,
  72. opacity: options.opacity
  73. })
  74. .appendTo('body');
  75. }
  76. });
  77. };
  78.  
  79. $.fn.shadow.defaults = {
  80. copies: 5,
  81. opacity: 0.1,
  82. copyOffset: function(index) {
  83. return {x: index, y: index};
  84. }
  85. };
  86. })(jQuery);
  87.  
  88. /******************************************************************************
  89. .tooltip()
  90. A simple jQuery UI tooltip widget.
  91. ******************************************************************************/
  92. (function($) {
  93. $.widget('ljq.tooltip', {
  94. options: {
  95. offsetX: 10,
  96. offsetY: 10,
  97. content: function() {
  98. return $(this).data('tooltip-text');
  99. }
  100. },
  101.  
  102. _create: function() {
  103. this._tooltipDiv = $('<div></div>')
  104. .addClass('ljq-tooltip-text ui-widget ui-state-highlight ui-corner-all')
  105. .hide().appendTo('body');
  106. this.element
  107. .addClass('ljq-tooltip-trigger')
  108. .on('mouseenter.ljq-tooltip', $.proxy(this._open, this))
  109. .on('mouseleave.ljq-tooltip', $.proxy(this._close, this));
  110. },
  111.  
  112. destroy: function() {
  113. this._tooltipDiv.remove();
  114. this.element
  115. .removeClass('ljq-tooltip-trigger')
  116. .off('.ljq-tooltip');
  117. $.Widget.prototype.destroy.apply(this, arguments);
  118. },
  119.  
  120. open: function() {
  121. this._open();
  122. },
  123.  
  124. close: function() {
  125. this._close();
  126. },
  127.  
  128. _open: function() {
  129. if (!this.options.disabled) {
  130. var elementOffset = this.element.offset();
  131. this._tooltipDiv.css({
  132. position: 'absolute',
  133. left: elementOffset.left + this.options.offsetX,
  134. top: elementOffset.top + this.element.height() + this.options.offsetY
  135. }).text(this.options.content.call(this.element[0]));
  136. this._tooltipDiv.show();
  137. this._trigger('open');
  138. }
  139. },
  140.  
  141. _close: function() {
  142. this._tooltipDiv.hide();
  143. this._trigger('close');
  144. }
  145. });
  146. })(jQuery);
  147.  
  148. /******************************************************************************
  149. End plugin code; begin custom script code.
  150. ******************************************************************************/
  151. $(document).ready(function() {
  152. var $inventory = $('#inventory tbody');
  153. var quantities = $inventory.find('td:nth-child(2)')
  154. .map(function(index, qty) {
  155. return $(qty).text();
  156. }).get();
  157.  
  158. var prices = $inventory.find('td:nth-child(3)')
  159. .map(function(index, qty) {
  160. return $(qty).text();
  161. }).get();
  162.  
  163. var sum = $.mathUtils.sum(quantities);
  164. var average = $.mathUtils.average(prices);
  165. $('#sum').find('td:nth-child(2)').text(sum);
  166. $('#average').find('td:nth-child(3)').text(average.toFixed(2));
  167.  
  168. $('table').click(function() {
  169. $('tr').swapClass('one', 'two');
  170. });
  171.  
  172. $.fn.shadow.defaults.copies = 10;
  173. $('h1').shadow({
  174. copyOffset: function(index) {
  175. return {x: -index, y: index};
  176. }
  177. });
  178.  
  179. $('a').tooltip();
  180. });

2.

  1. <!DOCTYPE html>
  2.  
  3. <html lang="en">
  4. <head>
  5. <meta charset="utf-8">
  6. <title>Developing Plugins</title>
  7.  
  8. <link rel="stylesheet" href="08.css" type="text/css" />
  9. <link rel="stylesheet" href="ui-themes/smoothness/jquery-ui-1.10.0.custom.css" type="text/css" />
  10.  
  11. <script src="jquery.js"></script>
  12. <script src="jquery-ui-1.10.0.custom.min.js"></script>
  13. <script src="08.js"></script>
  14.  
  15. </head>
  16. <body>
  17. <div id="container">
  18. <h1>Inventory</h1>
  19. <table id="inventory">
  20. <thead>
  21. <tr class="one">
  22. <th>Product</th>
  23. <th>Quantity</th>
  24. <th>Price</th>
  25. </tr>
  26. </thead>
  27. <tfoot>
  28. <tr class="two" id="sum">
  29. <td>Total</td>
  30. <td></td>
  31. <td></td>
  32. </tr>
  33. <tr id="average">
  34. <td>Average</td>
  35. <td></td>
  36. <td></td>
  37. </tr>
  38. </tfoot>
  39. <tbody>
  40. <tr>
  41. <td><a href="spam.html" data-tooltip-text="Nutritious and delicious!">Spam</a></td>
  42. <td>4</td>
  43. <td>2.50</td>
  44. </tr>
  45. <tr>
  46. <td><a href="egg.html" data-tooltip-text="Farm fresh or scrambled!">Egg</a></td>
  47. <td>12</td>
  48. <td>4.32</td>
  49. </tr>
  50. <tr>
  51. <td><a href="gourmet-spam.html" data-tooltip-text="Chef Hermann's recipe.">Gourmet Spam</a></td>
  52. <td>14</td>
  53. <td>7.89</td>
  54. </tr>
  55. </tbody>
  56. </table>
  57. </div>
  58. </body>
  59. </html>

jQuery基础教程-第8章-004完整代码的更多相关文章

  1. 总结: 《jQuery基础教程》 1-4章

    前言: 因为公司的项目用到了jQuery+Bootstrap,而Bootstrap基于jQuery,突然发现自己只是很久前看过jQuery的视频教程,对jQuery的一些API有一些了解,在使用中还是 ...

  2. jQuery基础教程-第8章-003Providing flexible method parameters

    一.The options object 1.增加阴影效果 (function($) { $.fn.shadow = function() { return this.each(function() ...

  3. jQuery基础教程-第8章-002Adding jQuery object methods

    一.Object method context 1.We have seen that adding global functions requires extending the jQuery ob ...

  4. jQuery基础教程-第8章-001Adding new global functions

    一. 1.To add a function to the jQuery namespace, we can just assign the new function asa property of ...

  5. 《jQuery基础教程(第四版)》学习笔记

    本书代码参考:Learning jQuery Code Listing Browser 原书: jQuery基础教程 目录: 第2章 选择元素 1. 使用$()函数 2. 选择符 3. DOM遍历方法 ...

  6. 《jQuery基础教程》读书笔记

    最近在看<jQuery基础教程>这本书,做了点读书笔记以备回顾,不定期更新. 第一章第二章比较基础,就此略过了... 第三章 事件 jQuery中$(document).ready()与j ...

  7. Objective-C 基础教程第五章,复合

    目录 Objective-C 基础教程第五章,复合 什么是复合? Car程序 自定义NSLog() 存取方法get Set Tires(轮胎) 存取方法 Car类代码的其他变化 扩展Car程序 复合还 ...

  8. Objective-C 基础教程第七章,深入理解Xcode

    目录 Object-C 基础教程第七章,深入理解Xcode 0x00 前言 0x01 创建工程界面 0x02 主程序界面 ①顶部 Top Test(测试) Profile(动态分析) Analyze( ...

  9. jquery基础教程读书总结

    最近静下心来看书才深刻的体会到:看书真的很重要,只有看书才能让你有心思静下心来思考. 重温<jquery基础教程> 一.事件 主要掌握常见的事件以及理解jquery的事件处理机制. 需要注 ...

随机推荐

  1. opencv 学习笔记集锦

    整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...

  2. linux下修改ip地址

    1.more  /etc/sysconfig/network-scripts/ifcfg-eth0 2.ifconfig eth0 192.168.1.211 netmask 255.255.255. ...

  3. Ambari的资源池管理

    操作: YARN→Config→Advanced→Schedule capacity-scheduler=null yarn.scheduler.capacity.default.minimum-us ...

  4. SQL语言分为五大类

    SQL语言分为五大类:DDL(数据定义语言) - Create.Alter.Drop 这些语句自动提交,无需用Commit提交.DQL(数据查询语言) - Select 查询语句不存在提交问题.DML ...

  5. 常用JavaScript操作页面元素的方法

    1.取得dropdownlist的选中值 var ddl =document.getElementById('<%=ddlusers.ClientID%>'); var index = d ...

  6. cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'

    <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springfr ...

  7. 【转】 Pro Android学习笔记(九七):BroadcastReceiver(1):基础小例子

    目录(?)[-] 基础小例子 发送Broadcast intent 运行情况 应用间的广播 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog ...

  8. linux 定时脚本任务的创建

    参考资料https://my.oschina.net/xsh1208/blog/512810 定时脚本任务创建过程 1. 启动/终止 crontab 服务 一般使用这个命令/sbin/service ...

  9. 1032 Sharing

    题意:寻找两个链表的首个公共结点,输出其地址. 思路: 方法1.  如果LinkList1比LinkList2长,则让LinkList1先偏移(len1-len2)个结点,然后,让两个链表的的工作指针 ...

  10. spring boot 集成 rabbitmq

    1.使用默认的AmqpTemplate生产消费pojo时,pojo需要implement Serializable,否则会抛出org.springframework.amqp.AmqpExceptio ...