自己动手开发jQuery插件全面解析 jquery插件开发方法
- jQuery.foo = function() {
- alert('This is a test. This is only a test.');
- };
- jQuery.foo = function() {
- alert('This is a test. This is only a test.');
- };
- jQuery.bar = function(param) {
- alert('This function takes a parameter, which is "' + param + '".');
- };
- 调用时和一个函数的一样的:jQuery.foo();jQuery.bar();或者$.foo();$.bar('bar');
- jQuery.extend({
- foo: function() {
- alert('This is a test. This is only a test.');
- },
- bar: function(param) {
- alert('This function takes a parameter, which is "' + param +'".');
- }
- });
- jQuery.myPlugin = {
- foo:function() {
- alert('This is a test. This is only a test.');
- },
- bar:function(param) {
- alert('This function takes a parameter, which is "' + param + '".');
- }
- };
- 采用命名空间的函数仍然是全局函数,调用时采用的方法:
- $.myPlugin.foo();
- $.myPlugin.bar('baz');
- (function($){
- $.fn.extend({
- pluginName:function(opt,callback){ //这个callback还没有理解
- // Our plugin implementation code goes here.
- }
- })
- })(jQuery);
形式2:
- (function($) {
- $.fn.pluginName = function() {
- // Our plugin implementation code goes here.
- };
- })(jQuery);
上面定义了一个jQuery函数,形参是$,函数定义完成之后,把jQuery这个实参传递进去.立即调用执行。这样的好处是,我们在写jQuery插件时,也可以使用$这个别名,而不会与prototype引起冲突.
- $.fn.hilight = function() {
- // Our plugin implementation code goes here.
- };
- 我们的插件通过这样被调用:
- $('#myDiv').hilight();
2.2 接受options参数以控制插件的行为
- // plugin definition
- $.fn.hilight = function(options) {
- var defaults = {
- foreground: 'red',
- background: 'yellow'
- };
- // Extend our default options with those provided.
- var opts = $.extend(defaults, options); //这里是对defaults对象进行扩展
- // Our plugin implementation code goes here.
- };
- 我们的插件可以这样被调用:
- $('#myDiv').hilight({
- foreground: 'blue'
- });
- // plugin definition
- $.fn.hilight = function(options) {
- // Extend our default options with those provided.
- // Note that the first arg to extend is an empty object -
- // this is to keep from overriding our "defaults" object.
- var opts = $.extend({}, $.fn.hilight.defaults, options); //这是扩展$.fn.hilight.defaults名空间下进行扩展,这样就可以满足客户端的链式调用
- // Our plugin implementation code goes here.
- };
- // plugin defaults - added as a property on our plugin function
- $.fn.hilight.defaults = { //在hilight的名空间下,有添加了一个名空间
- foreground: 'red',
- background: 'yellow'
- };
- 现在使用者可以包含像这样的一行在他们的脚本里:
- //这个只需要调用一次,且不一定要在ready块中调用
- $.fn.hilight.defaults.foreground = 'blue';
- 接下来我们可以像这样使用插件的方法,结果它设置蓝色的前景色:
- $('#myDiv').hilight();
- // plugin definition
- $.fn.hilight = function(options) {
- // iterate and reformat each matched element
- return this.each(function() {
- var $this = $(this); //最好写成这样,因为$this,一看就知道是jquery对象了,不会让人以为是原始对象
- // ...
- var markup = $this.html();
- // call our format function
- markup = $.fn.hilight.format(markup);
- $this.html(markup);
- });
- };
- // define our format function
- $.fn.hilight.format = function(txt) {
- return '<strong>' + txt + '</strong>';
- };
我们很容易的支持options对象中的其他的属性通过允许一个回调函数来覆盖默认的设置。这是另外一个出色的方法来修改你的插件。这里展示的技巧是进一步有效的暴露format函数进而让他能被重新定义。通过这技巧,是其他人能够传递他们自己设置来覆盖你的插件,换句话说,这样其他人也能够为你的插件写插件。
考虑到这个篇文章中我们建立的无用的插件,你也许想知道究竟什么时候这些会有用。一个真实的例子是Cycle插件.这个Cycle插件是一个滑动显示插件,他能支持许多内部变换作用到滚动,滑动,渐变消失等。但是实际上,没有办法定义也许会应用到滑动变化上每种类型的效果。那是这种扩展性有用的地方。 Cycle插件对使用者暴露"transitions"对象,使他们添加自己变换定义。插件中定义就像这样:
- (function($) {
- // plugin definition
- $.fn.hilight = function(options) {
- debug(this);
- // ...
- };
- // private function for debugging
- function debug($obj) {
- if (window.console && window.console.log)
- window.console.log('hilight selection count: ' + $obj.size());
- };
- // ...
- })(jQuery);
- $.fn.hilight = function(options) {
- // ...
- // build main options before element iteration
- var opts = $.extend({}, $.fn.hilight.defaults, options);
- return this.each(function() {
- var $this = $(this);
- // build element specific options
- var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
- //...
- <!-- markup -->
- <div class="hilight { background: 'red', foreground: 'white' }">
- Have a nice day!
- </div>
- <div class="hilight { foreground: 'orange' }">
- Have a nice day!
- </div>
- <div class="hilight { background: 'green' }">
- Have a nice day!
- </div>
- 现在我们能高亮哪些div仅使用一行脚本:
- $('.hilight').hilight();
下面使我们的例子完成后的代码:
- // 创建一个闭包
- (function($) {
- // 插件的定义
- $.fn.hilight = function(options) {
- debug(this);
- // build main options before element iteration
- var opts = $.extend({}, $.fn.hilight.defaults, options);
- // iterate and reformat each matched element
- return this.each(function() {
- $this = $(this);
- // build element specific options
- var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
- // update element styles
- $this.css({
- backgroundColor: o.background,
- color: o.foreground
- });
- var markup = $this.html();
- // call our format function
- markup = $.fn.hilight.format(markup);
- $this.html(markup);
- });
- };
- // 私有函数:debugging
- function debug($obj) {
- if (window.console && window.console.log)
- window.console.log('hilight selection count: ' + $obj.size());
- };
- // 定义暴露format函数
- $.fn.hilight.format = function(txt) {
- return '<strong>' + txt + '</strong>';
- };
- // 插件的defaults
- $.fn.hilight.defaults = {
- foreground: 'red',
- background: 'yellow'
- };
- // 闭包结束
- })(jQuery);
jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。
自己动手开发jQuery插件全面解析 jquery插件开发方法的更多相关文章
- 转 jquery插件--241个jquery插件—jquery插件大全
241个jquery插件—jquery插件大全 jquery插件jqueryautocompleteajaxjavascriptcoldfusion jQuery由美国人John Resig创建,至今 ...
- 【jQuery插件】用jQuery Masonry快速构建一个pinterest网站布局(转)
[jQuery插件]用jQuery Masonry快速构建一个pinterest网站布局 时间:2011年03月21日作者:愚人码头查看次数:29,744 views评论次数:25条评论 前段时间领导 ...
- JQUERY插件学习之jQuery UI
jQuery UI:http://jqueryui.com/ jQuery UI介绍: jQuery UI 是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互 ...
- jQuery 源码解析(三) pushStack方法 详解
该函数用于创建一个新的jQuery对象,然后将一个DOM元素集合加入到jQuery栈中,最后返回该jQuery对象,有三个参数,如下: elems Array类型 将要压入 jQuery 栈的数组元素 ...
- jQuery插件:图片放大镜--jQuery Zoom
本文转载于http://blog.csdn.net/xinhaozheng/article/details/4085644, 这是一款非常不错的给图片添加放大镜效果,可以应用在诸如zen cart,m ...
- 多日期选择jQuery插件 MultiDatesPicker for jQuery UI
Multiple-Dates-Picker-for-jQuery-UI是一个多日期选择的jquery控件. GIT源码: https://github.com/dubrox/Multiple-Da ...
- jQuery插件实例六:jQuery 前端分页
先来看看效果: 对于前端分页,关键是思路,和分页算法.本想多说两句,可又觉得没什么可说的,看代码吧: 如何使用? $("#pging").zPagination({ 'navEve ...
- 十七.jQuery源码解析之入口方法Sizzle(1)
函数Sizzle(selector,context,results,seed)用于查找与选择器表达式selector匹配的元素集合.该函数是选择器引擎的入口. 函数Sizzle执行的6个关键步骤如下: ...
- 使用 Eclipse 可视化插件 windowbuilder 进行Java GUI开发(插件安装的两种方法)
对于Java GUI开发 其实最方便的方法是用插件制作,当然先了解完代码原理是最好的. eclispe安装windowbuilder有两种方式,一种是离线安装,一种是在线安装. 一.第一种在线安装: ...
随机推荐
- decimal.ToString("#0.00")与decimal.ToString("#.##")的区别
decimal decTemp = 2.1m; Console.WriteLine(decTemp.ToString("#0.00")); //输出2.10 Console.Wri ...
- Linux下查看进程IO工具iopp
Linux下的IO检测工具最常用的是iostat,不过iostat只能查看到总的IO情况.如果要细看具体那一个程序点用的IO较高,可以使用iotop .不过iotop对内核版本和Python版本有要求 ...
- 51nod 编辑距离 + 滚动数组优化
这道题一开始觉得增加和删除会移动字符串的位置很不好做 两个字符串dp状态一般是第一个前i个和第二个前j个 #include<cstdio> #include<algorithm> ...
- iterator的使用和封个问题
这篇文章的内容还是不错的: http://www.cnblogs.com/zhuyf87/archive/2012/12/08/2808290.html for (vector<int>: ...
- 【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】
[008-String to Integer (atoi) (字符串转成整数)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Implement atoi to co ...
- hadoop实验:求气象数据的最低温度
1.下载部分数据.由于实验就仅仅下载2003年的部分气象数据 2.通过zcat *gz > sample.txt命令解压重定向 [hadoop@Master test_data]$ zcat * ...
- Reuse Is About People and Education, Not Just Architecture
 Reuse Is About People and Education, Not Just Architecture Jeremy Meyer you MigHT AdopT THE AppRoA ...
- 60.浅谈nodejs中的Crypto模块
转自:https://www.cnblogs.com/c-and-unity/articles/4552059.html node.js的crypto在0.8版本并没有改版多少,这个模块的主要功能是加 ...
- MathType下载和安装(与Visio搭配使用)
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- 关于cook操作
http://www.cnblogs.com/fishtreeyu/archive/2011/10/06/2200280.html