jQuery基础教程-第8章-002Adding jQuery object methods
一、Object method context
1.We have seen that adding global functions requires extending the jQuery object with new methods. Adding instance methods is similar, but we instead extend the jQuery.fn object(The jQuery.fn object is an alias to jQuery.prototype,provided for conciseness.):
 jQuery.fn.myMethod = function() {
     alert('Nothing happens.');
 };
We can then call this new method from our code after using any selector expression:
 $('div').myMethod();
Our alert is displayed (once for each <div> in the document) when we invoke the method.
2.交换CSS的类样式
// Unfinished code
(function($) {
$.fn.swapClass = function(class1, class2) {
if (this.hasClass(class1)) {
this.removeClass(class1).addClass(class2);
} else if (this.hasClass(class2)) {
this.removeClass(class2).addClass(class1);
}
};
})(jQuery);
$(document).ready(function() {
$('table').click(function() {
$('tr').swapClass('one', 'two');
});
});
二、Implicit iteration
 (function($) {
     $.fn.swapClass = function(class1, class2) {
         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);
The meaning of "this"
Caution: The keyword this refers to a jQuery object within the object method's body, but refers to a DOM element within the .each() invocation.
三、Enabling method chaining
 (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);
jQuery基础教程-第8章-002Adding jQuery object methods的更多相关文章
- 总结: 《jQuery基础教程》 1-4章
		
前言: 因为公司的项目用到了jQuery+Bootstrap,而Bootstrap基于jQuery,突然发现自己只是很久前看过jQuery的视频教程,对jQuery的一些API有一些了解,在使用中还是 ...
 - jQuery基础教程-第8章-004完整代码
		
1. /****************************************************************************** Our plugin code c ...
 - jQuery基础教程-第8章-003Providing flexible method parameters
		
一.The options object 1.增加阴影效果 (function($) { $.fn.shadow = function() { return this.each(function() ...
 - 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--jQuery基础--jQuery基础课程--第1章 初识jQuery
		
环境搭建 搭建一个jQuery的开发环境非常方便,可以通过下列几个步骤进行. 下载jQuery文件库 在jQuery的官方网站(http://jquery.com)中,下载最新版本的jQuery文件库 ...
 - 《jQuery基础教程(第四版)》学习笔记
		
本书代码参考:Learning jQuery Code Listing Browser 原书: jQuery基础教程 目录: 第2章 选择元素 1. 使用$()函数 2. 选择符 3. DOM遍历方法 ...
 - 《jQuery基础教程》读书笔记
		
最近在看<jQuery基础教程>这本书,做了点读书笔记以备回顾,不定期更新. 第一章第二章比较基础,就此略过了... 第三章 事件 jQuery中$(document).ready()与j ...
 - jquery基础教程读书总结
		
最近静下心来看书才深刻的体会到:看书真的很重要,只有看书才能让你有心思静下心来思考. 重温<jquery基础教程> 一.事件 主要掌握常见的事件以及理解jquery的事件处理机制. 需要注 ...
 - Objective-C 基础教程第三章,面向对象编程基础知
		
目录 Objective-C 基础教程第三章,面向对象编程基础知 0x00 前言 0x01 间接(indirection) 0x02 面向对象编程中使用间接 面向过程编程 面向对象编程 0x03 OC ...
 
随机推荐
- Adobe Fireworks CS6是一款集网页图片设计、制作与编辑为一体的专业软件
			
Adobe Fireworks CS6是一款集网页图片设计.制作与编辑为一体的专业软件,它不仅可以轻松制作出各种动感的Gif.动态按钮.动态翻转等网络图片,还可以轻松实现大图切割,让网页加载的图片显示 ...
 - SqlServer 临时表
			
SqlServer中临时表分为两种:一种是局部(本地)临时表,用#TableName表示.一种是全局(服务器)临时表,用##TableName表示. 创建临时表: 1. create table #T ...
 - The type org.springframework.context.ConfigurableApplicationContext cannot be resolved问题解决
			
在搭建maven项目的时候,有时候会报这样的问题. The type org.springframework.context.ConfigurableApplicationContext cannot ...
 - MTK驱动移植相关路径
			
转自:http://blog.csdn.net/yicao821/article/details/52314578 一.Flash兼容 bootable/bootloader/preloader/to ...
 - Use the dkms from EPEL when install CUDA Toolkits on CentOS
			
###Use the dkms from EPEL. yum install epel-release yum install dkms # download the rpm from the NVi ...
 - java中String和char的区别
			
首先来看一下Java的数据类型.Java 包括两种数据类型: 1.原始数据类型(primitive data type):byte,short, char, int, long,float,doubl ...
 - Python3中的http.client模块
			
http 模块简介 Python3 中的 http 包中含有几个用来开发 HTTP 协议的模块. http.client 是一个底层的 HTTP 协议客户端,被更高层的 urllib.request ...
 - AzureStack混合云大数据解决方案
			
AzureStack是Azure的私有云解决方案.AzureStack可以帮助用户实现混合云的部署模式. 本文将介绍混合云的模式下,Azure作为计算资源,AzureStack作为存储资源.如下图: ...
 - ror中间一些单复数的规则
			
基于rails generate生成的东西里面,有一些单复数的规则刚开始很不理解,觉得很复杂,容易弄错,特此记录 model 实际是对于数据库数据的对象化,只体现单个对象,比如模型user有name和 ...
 - npm run dev报错,events.js:160 throw er; // Unhandled 'error' event
			
错误代码如下: vue-project@1.0.0 dev E:MySoftwaretestGitwebpackvue-projectnode build/dev-server.js "80 ...