jQuery插件开发

jQuery插件的开发包括两种:
一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法。jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级别的插件开发,即给jQuery对象添加方法。下面就两种函数的开发做详细的说明。

1、类级别的插件开发-为jQuery添加静态方法
类级别的插件开发最直接的理解就是给jQuery类添加类方法,可以理解为添加静态方法。典型的例子就是$.AJAX()这个函数,将函数定义于jQuery的命名空间中。关于类级别的插件开发可以采用如下几种形式进行扩展:

1.1 添加一个新的全局函数
添加一个全局函数,我们只需如下定义:

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>jQuery插件开发</title>
    <script type="text/javascript" src="validate/jquery-1.6.2.min.js"></script>    
    <script type="text/javascript">
    $(function(){
        //添加一个新的全局函数
        jQuery.foo = function() { 
            alert('This is a test. This is only a test.');
        };  
        
        $("#btn").click(function(){
            //调用新的全局函数
            $.foo();//或写成:jQuery.foo();
        });
    });
    </script>

  </head>
  
  <body>
      <input type="button" id="btn" value="点击"/>
  </body>
</html>
复制代码

1.2 增加多个全局函数
添加多个全局函数,可采用如下定义:

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>jQuery插件开发</title>
    <script type="text/javascript" src="validate/jquery-1.6.2.min.js"></script>    
    <script type="text/javascript">
    $(function(){
        //增加多个全局函数
        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 + '".');  
        };   
        
        $("#btn").click(function(){
            //调用多个全局函数
            $.foo();//或写成:jQuery.foo();
            $.bar('bar'); //或写成:jQuery.bar('bar');
        });
    });
    </script>

  </head>
  
  <body>
      <input type="button" id="btn" value="点击"/>
  </body>
</html>
复制代码

1.3 使用jQuery.extend(object); 

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>jQuery插件开发</title>
    <script type="text/javascript" src="validate/jquery-1.6.2.min.js"></script>    
    <script type="text/javascript">
    $(function(){
        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 +'".');    
            }   
        });
        
        
        $("#btn").click(function(){
            //调用多个全局函数
            $.foo();//或写成:jQuery.foo();
            $.bar('bar'); //或写成:jQuery.bar('bar');
        });
    });
    </script>

  </head>
  
  <body>
      <input type="button" id="btn" value="点击"/>
  </body>
</html>
复制代码

1.4 使用命名空间
虽然在jQuery命名空间中,我们禁止使用了大量的javaScript函数名和变量名。但是仍然不可避免某些函数或变量名将于其他jQuery插件冲突,因此我们习惯将一些方法封装到另一个自定义的命名空间。

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>jQuery插件开发</title>
    <script type="text/javascript" src="validate/jquery-1.6.2.min.js"></script>    
    <script type="text/javascript">
    $(function(){
        jQuery.myPlugin = {        
            foo:function() {        
                alert('This is a test. ');        
            },        
            bar:function(param) {        
                alert('This function takes a parameter, which is "' + param + '".');  
            }       
        };
        
        $("#btn").click(function(){
            //调用多个全局函数
            $.myPlugin.foo();//或写成:jQuery.myPlugin.foo();
            $.myPlugin.bar('bar'); //或写成:jQuery.myPlugin.bar('bar'); 
        });
    });
    </script>

  </head>
  
  <body>
      <input type="button" id="btn" value="点击"/>
  </body>
</html>
复制代码

通过这个技巧(使用独立的插件名),我们可以避免命名空间内函数的冲突。

2、对象级别的插件开发-给jQuery对象添加方法
对象级别的插件开发需要如下的两种形式:、
形式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); 

上面定义了一个jQuery函数,形参是$,函数定义完成之后,把jQuery这个实参传递进去。立即调用执行。这样的好处是,我们在写jQuery插件时,也可以使用$这个别名,而不会与prototype引起冲突。例如:

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>jQuery插件开发</title>
    <script type="text/javascript" src="validate/jquery-1.6.2.min.js"></script>    
    <script type="text/javascript">
    $(function(){
        //插件定义-方法一
        (function($) {
            $.fn.alertWhileClick = function() {
                $(this).click(function (){
                    alert($(this).val() + "2");
                });
            }
        })(jQuery); 
        
        //插件定义-方法二
        (function($) {
            $.fn.extend({ 
                alertWhileClick2:function(){
                    $(this).click(function(){
                        alert("2");
                    });
                }
            })
        })(jQuery);         
        
        //调用插件
        $('#btn').alertWhileClick();
          $('#btn').alertWhileClick2();
    });
    </script>

  </head>
  
  <body>
      <input id="btn" type="button" value="点击"/>
  </body>
</html>
复制代码

注意:
这里使用了闭包的方式

(function($) {
   // Code
})(jQuery);

上面的代码等价于下面的代码

var jQueryFucntion=function($) {
   // Code
} 
jQueryFucntion(jQuery);

这样定义之后,很大程序上毕免了第三方的误操作,从而破坏封装性。如比较常见的场景就是防止'$'与其它js库的冲突。

原文地址:https://www.cnblogs.com/estellez/p/4554606.html