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 ...
随机推荐
- CDlinux无线破解系统
cdlinux是一款功能非常强大的无线密码破解器,cdlinux兼容pin软件,不用抓取握手包.不用跑字典.不用客户端在线就能够破解无线路由器的密码,轻轻松松帮助用户达到蹭网的目的. 基本简介 cdl ...
- map/reduce类简单介绍
在Hadoop的mapper类中,有4个主要的函数,分别是:setup,clearup,map,run.代码如下: protected void setup(Context context) thro ...
- LA2218 Triathlon
题意 PDF 分析 设出长度\(x,y,1-x-y\),就是关于它们的二元一次不等式,判断有没有解. 可以用半平面交来解决. x/V[i]+y/U[i]+(1-x-y)/W[i] < x/V[j ...
- 探秘VB.net中的shared与static
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huyuyang6688/article/details/28230345 简单了解了一 ...
- poj 3128 Leonardo's Notebook——思路(置换)
题目:http://poj.org/problem?id=3128 从环的角度考虑. 原来有奇数个点的环,现在点数不变: 原来有偶数个点的环(设有 k 个点),现在变成两个大小为 k/2 的环. 所以 ...
- CentOS7上部署https
目前很多浏览器都加强了html都安全性,要求配置https. 下面都例子是在CentOS7上的Apache配置https都过程. 一.生成证书 用OpenSSL生成key和证书: mkdir /etc ...
- memcache常见问题及解答
memcached的cache机制是怎样的? Memcached主要的cache机制是LRU(最近最少用)算法+超时失效.当您存数据到memcached中,可以指定该数据在缓存中可以呆多久Which ...
- Java调用Groovy
记录一下 http://docs.groovy-lang.org/latest/html/documentation/guide-integrating.html
- Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)
模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...
- thinkphp中配置信息的二维数组设置与使用
有时候配置信息是二维数组 1.配置 <?php return array ( // 阿里大鱼短信配置 'dayu_appkey'=>'xxx', 'dayu_secretKey'=> ...