English 中文(简体)
将事件添加到Jquery插件
原标题:add event to Jquery Plugin

I have a JQuery plugin and I want to add some event to it. Any help, tips, or tutorial?

最佳回答

正如我正确理解的那样,如果你有某种关闭弹出窗口的按钮,并且你需要一个关闭后触发的功能。

(function($) {
  $.fn.somePlugin = function(options) {
    // do some stuff with popup
    $( #closeButton ).click(function(e) {
      //close popup
      if (options.onAfterClose !== undefined) {
         options.onAfterClose(e);
      }
    });
  };
})(jQuery);

它只是从点击事件中传递事件。

$( #someId ).somePlugin({
    onAfterClose: function(e) {
        alert( after close );
    }
});
问题回答

在您的jquery插件中,您必须做的第一件事是在插件代码中的某个位置触发您的自定义事件:

$(“#someElement”).trigger(“someSpecialEvent”);

如果需要,还可以在触发器函数内部传递数据。

$("#someElement").trigger("someSpecialEvent", "this does not need to be a string");

接下来,在插件代码的其他地方为自定义事件创建一个事件处理程序:

$("#someElement").bind("someSpecialEvent", function(event, data) {
    //If you had passed anything in your trigger function, you can grab it using the second parameter in the callback function.
});

然后,您可以允许用户将回调函数作为如下选项传递:

$("#sampleElement").myPlugin({ 
    someEvent: function() {
        //user logic
    }
});

最后,在插件代码中,您可以通过几种不同的方式调用用户的函数。例如:

$.fn.samplePlugin = function(options) {
        options = $.extend({}, $.fn.samplePlugin.options, options);
        $("sampleElement").bind("specialEvent", function() {
             options.someEvent.call();
        });
 }

jQuery文档具备触发事件的一切功能

使用$.bind()附加到事件的任何元素都将被通知事件已被触发并执行其代码。

示例:

$(document).trigger( my.event );

jQuery支持按名称命名的事件,因此您可以将事件命名为mypluginName.eventName,以确保没有其他插件干扰您的事件;)

简单而干净,尽管要小心,但文档指出:

Although .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.

天气不错

$(“#someElement”).on(“someSpecialEvent”,函数(event){

});





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签