jquery插件开发及 jquery自定义函数
来源:互联网 发布:mysql安装包下载 64 编辑:程序博客网 时间:2024/05/01 13:33
一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法。jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级别的插件开发,即给jQuery对象添加方法。下面就两种函数的开发做详细的说明。
jQuery为开发插件提拱了两个方法,分别是:
JavaScript代码
- jQuery.fn.extend(object);
- jQuery.extend(object);
jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。
jQuery.fn.extend(object);给jQuery对象添加方法。
fn 是什么东西呢。查看jQuery代码,就不难发现。
JavaScript代码
- jQuery.fn = jQuery.prototype = {
- init: function( selector, context ) {//….
- //……
- };
原来 jQuery.fn = jQuery.prototype.对prototype肯定不会陌生啦。
虽然 javascript 没有明确的类的概念,但是用类来理解它,会更方便。
jQuery便是一个封装得非常好的类,比如我们用 语句 $(“#btn1″) 会生成一个 jQuery类的实例。
jQuery.extend(object); 为jQuery类添加添加类方法,可以理解为添加静态方法。如:
XML/HTML代码
- $.extend({
- add:function(a,b){return a+b;}
- });
便为 jQuery 添加一个为 add 的 “静态方法”,之后便可以在引入 jQuery 的地方,使用这个方法了,
- $.add(3,4); //return 7
jQuery.fn.extend(object); 对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”。jQuery类的实例可以使用这个“成员函数”。
比如我们要开发一个插件,做一个特殊的编辑框,当它被点击时,便alert 当前编辑框里的内容。可以这么做:
- $.fn.extend({
- alertWhileClick:function(){
- $(this).click(function(){
- alert($(this).val());
- });
- }
- });
- $(“#input1″).alertWhileClick(); //页面上为:<input id=”input1″ type=”text”/>
$(“#input1″) 为一个jQuery实例,当它调用成员方法 alertWhileClick后,便实现了扩展,每次被点击时它会先弹出目前编辑里的内容。
真实的开发过程中,当然不会做这么小白的插件,事实上jQuery提拱了丰富的操作文档,事件,CSS ,Ajax、效果的方法,结合这些方法,便可以开发出更加 Niubility 的插件。
1、类级别的插件开发
类级别的插件开发最直接的理解就是给jQuery类添加类方法,可以理解为添加静态方法。典型的例子就是$.AJAX()这个函数,将函数定义于jQuery的命名空间中。关于类级别的插件开发可以采用如下几种形式进行扩展:
1.1
添加一个全局函数,我们只需如下定义:
- jQuery.foo
= function() { - alert('This
is );a test. This is only a test.' - };
1.2
添加多个全局函数,可采用如下定义:
- 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');
1.3
- 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 +'".'); - }
- });
1.4
虽然在jQuery命名空间中,我们禁止使用了大量的javaScript函数名和变量名。但是仍然不可避免某些函数或变量名将于其他jQuery插件冲突,因此我们习惯将一些方法封装到另一个自定义的命名空间。
- 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');
通过这个技巧(使用独立的插件名),我们可以避免命名空间内函数的冲突。
2、对象级别的插件开发
对象级别的插件开发需要如下的两种形式:、
形式1:
- (function($){
- $.fn.extend({
- pluginName:function(opt,callback){
-
// Our plugin implementation code goes here. - }
- })
- })(jQuery);
形式2:
- (function($)
{ - $.fn.pluginName
= function() { -
// Our plugin implementation code goes here. - };
- })(jQuery);
2.1
这是一个单一插件的脚本。如果你的脚本中包含多个插件,或者互逆的插件(例如:
- $.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); -
// Our plugin implementation code goes here. - };
- 我们的插件可以这样被调用:
- $('#myDiv').hilight({
-
foreground: 'blue' - });
- jquery插件开发及 jquery自定义函数
- jquery插件开发及 jquery自定义函数
- jquery插件开发及 jquery自定义函数
- 【jQuery】jQuery自定义插件开发
- jquery 自定义插件开发
- jquery 自定义插件开发
- jQuery插件开发及jQuery.extend函数详解和jQuery.fn与jQuery.prototype区别
- jQuery插件开发及jQuery.extend函数详解和jQuery.fn与jQuery.prototype区别
- jQuery插件开发及jQuery.extend函数详解和jQuery.fn与jQuery.prototype区别
- jquery 自定义插件开发整理
- 根据JQuery开发自定义插件。
- jquery自定义插件开发总结
- [ jQuery ] jquery 自定义插件!
- jQuery自定义插件开发基础1——jQuery实用函数的实现
- jQuery自定义插件开发基础1——jQuery实用函数的实现 .
- jQuery自定义插件开发基础1——jQuery实用函数的实现
- jquery开发自定义的插件总结
- jquery开发自定义的插件总结
- 步步学习python之环境安装:Vagrant+Ubuntu+Emacs+Virtualenv+Python3开发环境的搭建(Mac电脑下)2
- 从Android Handle看 线程与对象、ThreadLocal
- iptables sample
- 菜鸟到大神的上位历程,即学即用走向人生巅峰
- Java基础知识IO流(字节流File读取操作)
- jquery插件开发及 jquery自定义函数
- Spring事务和异常回滚
- lsof -P
- 常用链接
- Bootstrap(六) 其它内置组件
- [BZOJ1251]序列终结者(splay)
- Eclipse 4.5+(Mars)安装SVN
- TextView长按复制实现方法
- 数据库的设计范式与疑惑