自定义JQuery插件的自定义事件 [英] Custom Event For Custom JQuery Plugin

查看:96
本文介绍了自定义JQuery插件的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您单击对象按钮时,我将其做成名为 removable 的jQuery插件,该插件会向上滑动,并应触发自定义事件,例如 onDone .

I made this jQuery plugin called removable when you click the objects button it slides up and should trigger a custom event like onDone.

这就是我所做的(编码格式基于jQuery的 http://docs.jquery.com/插件/创作):

Here's what I did (The codeing format is based on jQuery's http://docs.jquery.com/Plugins/Authoring):

init: function(){
    return this.each(function(){
        $('a', this).click(function(){
            $(this).parent().slideUp(function(){

                    // Somehow trigger the onDone method

            })
        });
    })
},
onDone: function(){
    // Default action
},

这就是我在调用插件时所做的

and this is what I've done when calling the plugin

   $('li').removable({
        onDone: function(){
          // Overwrite default action
        },
    })

这怎么办?

推荐答案

尝试一下.

(function($){
jQuery.fn.extend({
    removable: function(options) {
        var defaults = {  
             onDone: function(){alert('default action');}
        };  
        var options = $.extend(defaults, options);  
        return this.each(function() {   
            $('a', this).click(function(){
                 $(this).parent().slideUp(function(){
                 options.onDone.call();
            });
        });
});
    }
});
  })(jQuery);

  $('li').removable({
    onDone: function(){
      alert('Overwrite default action');
    },
})

这篇关于自定义JQuery插件的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆