一、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的更多相关文章

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

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

  2. jQuery基础教程-第8章-004完整代码

    1. /****************************************************************************** Our plugin code c ...

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

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

  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--jQuery基础--jQuery基础课程--第1章 初识jQuery

    环境搭建 搭建一个jQuery的开发环境非常方便,可以通过下列几个步骤进行. 下载jQuery文件库 在jQuery的官方网站(http://jquery.com)中,下载最新版本的jQuery文件库 ...

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

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

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

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

  8. jquery基础教程读书总结

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

  9. Objective-C 基础教程第三章,面向对象编程基础知

    目录 Objective-C 基础教程第三章,面向对象编程基础知 0x00 前言 0x01 间接(indirection) 0x02 面向对象编程中使用间接 面向过程编程 面向对象编程 0x03 OC ...

随机推荐

  1. SSH实现远程控制

    SSH(Secure Shell)是一种能够提供安全远程登录会话的协议,使用ssh可以在远程linux中执行命令. sshd服务提供两种安全验证的方法: (1)基于口令的安全验证:经过验证帐号与密码即 ...

  2. hibernate的list和iterate的区别

    一.先介绍一下java中的缓存系统JCS(java cache system)  1.JCS(Java Caching System)是一个对象Cache,它可以把Java对象缓存起来,提高那些访问频 ...

  3. 类数组对象:arguments

    在js中调用一个函数的时候,我们经常会给这个函数传递一些参数,js把传入到这个函数的全部参数存储在一个叫做arguments的东西里面,那它到底是什么呢? 一.描述 arguments 是一个对应于传 ...

  4. 简述MVC模式

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  5. composer 详解

    composer 详解 http://blog.csdn.net/panpan639944806/article/details/16808261 https://www.phpcomposer.co ...

  6. HTTP API 设计指南

    本指南描述了一系列 HTTP+JSON API 的设计实践, 来自并展开于 Heroku Platform API 的工作.本指南指导着Heroku内部API的开发,我们希望也能对Heroku以外的A ...

  7. 将java程序打包成exe文件

    一. 1. 项目右击,导出 jar文件 2. 下一步,选择 3. 完成 二. 1. 下载exe4j,并破解 2. 其他的步骤都好说,我主说这个步骤,一定要讲所有引用的jar包放到里面否则会报找不到文件 ...

  8. linux 输入子系统之电阻式触摸屏驱动

    一.输入子系统情景回忆ING...... 在Linux中,输入子系统是由输入子系统设备驱动层.输入子系统核心层(Input Core)和输入子系统事件处理层(Event Handler)组成.其中设备 ...

  9. docker image 转换 docker file

    这个是从每天的播报平台抓取到国外的信息发现的,感觉很实用. 博客原文,E文好的可以直接去看,https://samaritan.ai/blog/reversing-docker-images-into ...

  10. python学习笔记(三):文件操作和集合

    对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 文件基本操作: f = open('file.txt','r') #以只读方式打开一个 ...