码迷,mamicode.com
首页 > Web开发 > 详细

编写一个简单的Jquery插件

时间:2014-05-01 08:09:02      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:com   http   style   blog   class   div   img   c   code   java   log   

 

1、实现内容

    定义一个简单的jquery插件,alert传递进来的参数

 

2、插件js文件(jquery.showplugin.js)

mamicode.com,码迷
(function ($) {

    //定义插件中的方法
    var methods = {  //Object
        showName: function (name) {
            alert(‘Name:‘ + name);
        },
        showAge: function (age) {
            alert(‘Age‘ + age);
        }
    };

    //method方法名
    $.fn.showplugin = function (method) {
        if (methods[method]) {  //执行方法
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); //arguments可为字符串,也可为对象
        }
        else if (typeof method === ‘object‘ || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error(‘Method ‘ + method + ‘ does not exist on jQuery.showplugin‘);
        }
    };

})(jQuery);
mamicode.com,码迷

 

3、引入插件

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/Third/ShowPlugIn/jquery.showplugin.js" type="text/javascript"></script>

 

4、使用插件

       //showName:方法名
            //{}:传递参数,这里可以传递字符串,也可以传递对象
            $().showplugin(‘showName‘, {"k1":"k1Value","k2":"k2Value"}); 

 

 

编写一个简单的Jquery插件,码迷,mamicode.com

编写一个简单的Jquery插件

标签:com   http   style   blog   class   div   img   c   code   java   log   

原文地址:http://www.cnblogs.com/gossip/p/3701870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!