如何创建自定义事件并使用 jQuery 的 .on() 调用它们?

How to create custom events and call them using jQuery's .on()?

我正在尝试在 jQuery 中编写一个小型库,我想添加一些生命周期事件。我知道我必须在最后 return this; 才能继续链接。

到目前为止我的图书馆模型:

(function($))
  $.fn.myLibrary = function(){

    // some code here
    return this;

  }
)(jQuery)

这就是我希望的最终结果:

$('#selector').myLibrary({
    // some options
})
.on('myLibrary-deleting', function(){
    // some code
});

我不确定这个问题的措辞是否正确,但如何扩展 jQuery 的功能?

您不需要扩展 .on() 函数。只需触发您的自定义,即使您需要:

$(this).trigger('myLibrary-deleting')

并且 .on 将继续工作。

https://learn.jquery.com/events/introduction-to-custom-events/