【发布时间】:2018-11-01 11:24:51
【问题描述】:
我想覆盖 jquery 插件的初始化函数和插件的自定义函数(例如 html)。但没有任何工作。这是我的代码
提前致谢。
(function(jQuery) {
jQuery.mainplugin = function(element, options) {
var defaults = {};
this.init = function() {
this.settings = jQuery.extend({}, defaults, options);
alert('main')
// more code here
};
this.html = function() {
// main code here
}
this.init();
};
jQuery.fn.mainplugin = function(options) {
return this.each(function() {
if (undefined == jQuery(this).data('mainplugin')) {
var plugin = new jQuery.mainplugin(this, options);
jQuery(this).data('mainplugin', plugin);
}
});
};
})(jQuery);
这是我的覆盖代码:
$(document).ready(function($) {
$.fn.mainplugin.init = function() {
alert('override')
}
$.fn.mainplugin.html = function() {
alert('override')
}
$(".is-wrapper").mainplugin();
});
【问题讨论】:
-
$(".is-wrapper").mainplugin({ init: function() { //自定义函数 } });像这样@RoryMcCrossan ??
-
是的。我在下面添加了一个答案,给你一个完整的例子。
标签: jquery jquery-plugins overriding