本文介绍了jQuery:在插件中添加destroy方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个插件,我需要能够解除绑定并随意重新绑定。我怎么能在我的插件中的方法中将
打包,以便可以随意调用它?

I've created a plugin which I need to be able to unbind and rebind at will. How can I package this within a method in my plugin so that it can be called at will?

我的插件是这样的:

 (function($) {
     $.fn.myPlugin = function(options) {
            .................
            .................
            .................
            .................
            .................
            .................
      }; 
})( jQuery );

并称之为......

And called like...

$('#selector').myPlugin();

编辑:基本上,我想在我的插件中添加一个destroy方法

Basically, I want to add a destroy method to my plugin

推荐答案

这样的事情:

delete $.fn.MyPlugin;

您可以选择在您的插件中编写destroy方法,例如:

Optionally you can write destroy method into your plugin e.g.:

destroy: function() {
    this._destroy(); //or this.delete; depends on jQuery version
    this.element.unbind( this.eventNamespace )
    this.bindings.unbind( this.eventNamespace );
    //this.hoverable.removeClass( "hover state" );
    //this.focusable.removeClass( "focus state" );
}

这篇关于jQuery:在插件中添加destroy方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:47