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 ...
随机推荐
- rabbitmq java
package com.enniu.rabbitmq; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.AMQP.BasicPr ...
- sqlalchemy 学习(二)scoped session
数据库设计的难点之一,是session生命周期的管理问题.sqlalchemy提供了一个简单的session管理机制,即scoped session.它采用的注册模式.所谓的注册模式,简单来说,是指在 ...
- SSM框架——整合搭建流程
1.首先创建maven工程,使用哪种方式进行创建都可以,可以参考博主之前的文章: <两种方式创建Maven项目[方式二]><两种方式创建Maven项目[方式一]> 2.先看看搭 ...
- 区域存储网络(SAN)与 网络直接存储(NAS)
随着互联网及网络应用的飞速发展,数据信息存储系统所需处理的数据类型也呈爆炸性增长,这使数据信息存储系统面临前所未有的挑战.附加式网络存储装置(Network Attached Storage,缩写为N ...
- Ambari的资源池管理
操作: YARN→Config→Advanced→Schedule capacity-scheduler=null yarn.scheduler.capacity.default.minimum-us ...
- hdu 5730 Shell Necklace——多项式求逆+拆系数FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 可以用分治FFT.但自己只写了多项式求逆. 和COGS2259几乎很像.设A(x),指数是长度,系数 ...
- Linux安装微信
地址:http://www.toutiao.com/i6362126617556288001/#6649976-tsina-1-90079-4471e2b057b5019ad452c722f04bba ...
- str_split的用法(PHP学习)
str_split的用法: 先看看PHP手册是怎么说的 array str_split ( string $string [, int $split_length = 1 ] ) str_split返 ...
- Windows2008 R2上完全卸载Oracle操作步骤(转)
最近现场项目,碰到了好几次oracle数据库被损坏,而且无法恢复的问题,没办法,只好卸载重装了.oracle卸载确实麻烦,都是从网上查的方法, 为了方便以后查询,在此就做一下记录. Windows20 ...
- 直接通过ADO操作Access数据库
我在<VC知识库在线杂志>第十四期和第十五期上曾发表了两篇文章——“直接通过ODBC读.写Excel表格文件”和“直接通过DAO读.写Access文件”,先后给大家介绍了ODBC和DAO两 ...