编写一个简单的Jquery插件

 

1、实现内容

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

 

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

(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);

 

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"}); 

 

 

转载于:https://www.cnblogs.com/gossip/p/3701870.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值