jQuery基础教程-第8章-004完整代码
1.
- /******************************************************************************
- Our plugin code comes first in this document. Normally, plugins would
- appear in separate files named jquery.plugin-name.js, but for our examples
- it's convenient to place this plugin code in the same JavaScript file as
- the code that calls it.
- ******************************************************************************/
- /******************************************************************************
- $.sum()
- Return the total of the numeric values in an array/object.
- ******************************************************************************/
- (function($) {
- $.mathUtils = {
- sum: function(array) {
- var total = 0;
- $.each(array, function(index, value) {
- value = $.trim(value);
- value = parseFloat(value) || 0;
- total += value;
- });
- return total;
- },
- average: function(array) {
- if ($.isArray(array)) {
- return $.mathUtils.sum(array) / array.length;
- }
- return '';
- }
- };
- })(jQuery);
- /******************************************************************************
- .swapClass()
- Exchange one class for another on the selected elements.
- ******************************************************************************/
- (function($) {
- $.fn.swapClass = function(class1, class2) {
- return this.each(function() {
- var $element = $(this);
- if ($element.hasClass(class1)) {
- $element.removeClass(class1).addClass(class2);
- }
- else if ($element.hasClass(class2)) {
- $element.removeClass(class2).addClass(class1);
- }
- });
- };
- })(jQuery);
- /******************************************************************************
- .shadow()
- Create a shadow effect on any element by brute-force copying.
- ******************************************************************************/
- (function($) {
- $.fn.shadow = function(opts) {
- var options = $.extend({}, $.fn.shadow.defaults, opts);
- return this.each(function() {
- var $originalElement = $(this);
- for (var i = 0; i < options.copies; i++) {
- var offset = options.copyOffset(i);
- $originalElement
- .clone()
- .css({
- position: 'absolute',
- left: $originalElement.offset().left + offset.x,
- top: $originalElement.offset().top + offset.y,
- margin: 0,
- zIndex: -1,
- opacity: options.opacity
- })
- .appendTo('body');
- }
- });
- };
- $.fn.shadow.defaults = {
- copies: 5,
- opacity: 0.1,
- copyOffset: function(index) {
- return {x: index, y: index};
- }
- };
- })(jQuery);
- /******************************************************************************
- .tooltip()
- A simple jQuery UI tooltip widget.
- ******************************************************************************/
- (function($) {
- $.widget('ljq.tooltip', {
- options: {
- offsetX: 10,
- offsetY: 10,
- content: function() {
- return $(this).data('tooltip-text');
- }
- },
- _create: function() {
- this._tooltipDiv = $('<div></div>')
- .addClass('ljq-tooltip-text ui-widget ui-state-highlight ui-corner-all')
- .hide().appendTo('body');
- this.element
- .addClass('ljq-tooltip-trigger')
- .on('mouseenter.ljq-tooltip', $.proxy(this._open, this))
- .on('mouseleave.ljq-tooltip', $.proxy(this._close, this));
- },
- destroy: function() {
- this._tooltipDiv.remove();
- this.element
- .removeClass('ljq-tooltip-trigger')
- .off('.ljq-tooltip');
- $.Widget.prototype.destroy.apply(this, arguments);
- },
- open: function() {
- this._open();
- },
- close: function() {
- this._close();
- },
- _open: function() {
- if (!this.options.disabled) {
- var elementOffset = this.element.offset();
- this._tooltipDiv.css({
- position: 'absolute',
- left: elementOffset.left + this.options.offsetX,
- top: elementOffset.top + this.element.height() + this.options.offsetY
- }).text(this.options.content.call(this.element[0]));
- this._tooltipDiv.show();
- this._trigger('open');
- }
- },
- _close: function() {
- this._tooltipDiv.hide();
- this._trigger('close');
- }
- });
- })(jQuery);
- /******************************************************************************
- End plugin code; begin custom script code.
- ******************************************************************************/
- $(document).ready(function() {
- var $inventory = $('#inventory tbody');
- var quantities = $inventory.find('td:nth-child(2)')
- .map(function(index, qty) {
- return $(qty).text();
- }).get();
- var prices = $inventory.find('td:nth-child(3)')
- .map(function(index, qty) {
- return $(qty).text();
- }).get();
- var sum = $.mathUtils.sum(quantities);
- var average = $.mathUtils.average(prices);
- $('#sum').find('td:nth-child(2)').text(sum);
- $('#average').find('td:nth-child(3)').text(average.toFixed(2));
- $('table').click(function() {
- $('tr').swapClass('one', 'two');
- });
- $.fn.shadow.defaults.copies = 10;
- $('h1').shadow({
- copyOffset: function(index) {
- return {x: -index, y: index};
- }
- });
- $('a').tooltip();
- });
2.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Developing Plugins</title>
- <link rel="stylesheet" href="08.css" type="text/css" />
- <link rel="stylesheet" href="ui-themes/smoothness/jquery-ui-1.10.0.custom.css" type="text/css" />
- <script src="jquery.js"></script>
- <script src="jquery-ui-1.10.0.custom.min.js"></script>
- <script src="08.js"></script>
- </head>
- <body>
- <div id="container">
- <h1>Inventory</h1>
- <table id="inventory">
- <thead>
- <tr class="one">
- <th>Product</th>
- <th>Quantity</th>
- <th>Price</th>
- </tr>
- </thead>
- <tfoot>
- <tr class="two" id="sum">
- <td>Total</td>
- <td></td>
- <td></td>
- </tr>
- <tr id="average">
- <td>Average</td>
- <td></td>
- <td></td>
- </tr>
- </tfoot>
- <tbody>
- <tr>
- <td><a href="spam.html" data-tooltip-text="Nutritious and delicious!">Spam</a></td>
- <td>4</td>
- <td>2.50</td>
- </tr>
- <tr>
- <td><a href="egg.html" data-tooltip-text="Farm fresh or scrambled!">Egg</a></td>
- <td>12</td>
- <td>4.32</td>
- </tr>
- <tr>
- <td><a href="gourmet-spam.html" data-tooltip-text="Chef Hermann's recipe.">Gourmet Spam</a></td>
- <td>14</td>
- <td>7.89</td>
- </tr>
- </tbody>
- </table>
- </div>
- </body>
- </html>
jQuery基础教程-第8章-004完整代码的更多相关文章
- 总结: 《jQuery基础教程》 1-4章
前言: 因为公司的项目用到了jQuery+Bootstrap,而Bootstrap基于jQuery,突然发现自己只是很久前看过jQuery的视频教程,对jQuery的一些API有一些了解,在使用中还是 ...
- jQuery基础教程-第8章-003Providing flexible method parameters
一.The options object 1.增加阴影效果 (function($) { $.fn.shadow = function() { return this.each(function() ...
- jQuery基础教程-第8章-002Adding jQuery object methods
一.Object method context 1.We have seen that adding global functions requires extending the jQuery ob ...
- 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 ...
- 《jQuery基础教程(第四版)》学习笔记
本书代码参考:Learning jQuery Code Listing Browser 原书: jQuery基础教程 目录: 第2章 选择元素 1. 使用$()函数 2. 选择符 3. DOM遍历方法 ...
- 《jQuery基础教程》读书笔记
最近在看<jQuery基础教程>这本书,做了点读书笔记以备回顾,不定期更新. 第一章第二章比较基础,就此略过了... 第三章 事件 jQuery中$(document).ready()与j ...
- Objective-C 基础教程第五章,复合
目录 Objective-C 基础教程第五章,复合 什么是复合? Car程序 自定义NSLog() 存取方法get Set Tires(轮胎) 存取方法 Car类代码的其他变化 扩展Car程序 复合还 ...
- Objective-C 基础教程第七章,深入理解Xcode
目录 Object-C 基础教程第七章,深入理解Xcode 0x00 前言 0x01 创建工程界面 0x02 主程序界面 ①顶部 Top Test(测试) Profile(动态分析) Analyze( ...
- jquery基础教程读书总结
最近静下心来看书才深刻的体会到:看书真的很重要,只有看书才能让你有心思静下心来思考. 重温<jquery基础教程> 一.事件 主要掌握常见的事件以及理解jquery的事件处理机制. 需要注 ...
随机推荐
- opencv 学习笔记集锦
整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...
- linux下修改ip地址
1.more /etc/sysconfig/network-scripts/ifcfg-eth0 2.ifconfig eth0 192.168.1.211 netmask 255.255.255. ...
- Ambari的资源池管理
操作: YARN→Config→Advanced→Schedule capacity-scheduler=null yarn.scheduler.capacity.default.minimum-us ...
- SQL语言分为五大类
SQL语言分为五大类:DDL(数据定义语言) - Create.Alter.Drop 这些语句自动提交,无需用Commit提交.DQL(数据查询语言) - Select 查询语句不存在提交问题.DML ...
- 常用JavaScript操作页面元素的方法
1.取得dropdownlist的选中值 var ddl =document.getElementById('<%=ddlusers.ClientID%>'); var index = d ...
- 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 ...
- 【转】 Pro Android学习笔记(九七):BroadcastReceiver(1):基础小例子
目录(?)[-] 基础小例子 发送Broadcast intent 运行情况 应用间的广播 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog ...
- linux 定时脚本任务的创建
参考资料https://my.oschina.net/xsh1208/blog/512810 定时脚本任务创建过程 1. 启动/终止 crontab 服务 一般使用这个命令/sbin/service ...
- 1032 Sharing
题意:寻找两个链表的首个公共结点,输出其地址. 思路: 方法1. 如果LinkList1比LinkList2长,则让LinkList1先偏移(len1-len2)个结点,然后,让两个链表的的工作指针 ...
- spring boot 集成 rabbitmq
1.使用默认的AmqpTemplate生产消费pojo时,pojo需要implement Serializable,否则会抛出org.springframework.amqp.AmqpExceptio ...