10.1 Jquery
(function($) {
$.pluginName = function(element, options) {
var defaults = {
title: '',
content: '',
showOKBtn: 1, // 显示确定按钮
showCCBtn: 1, // 显示取消按钮
onFoo: function() {} // callback
}
var plugin = this;
plugin.settings = {}
var $element = $(element);
plugin.init = function(options) {
this.settings = $.extend({}, defaults, options);
this.initNode(options);
}
// public method.
plugin.show = function() {
// ...
}
plugin.hide = function() {
// ...
}
plugin.initNode = function(options) {
var $okBtn = $element.find(''),
$content = $element.find('');
// ....
// 部分逻辑
$content.text(plugin.settings.content);
$okBtn.on('click', $.proxy(this.onOk, this));
}
plugin.onOk = function(){
this.hide();
plugin.settings.onFoo();
}
plugin.init();
}
$.fn.pluginName = function(options) {
return this.each(function() {
if (undefined == $(this).data('pluginName')) {
var plugin = new $.pluginName(this, options);
$(this).data('pluginName', plugin);
}
});
}
})(jQuery);
// 使用
var template = '<div>...弹框html...</div>';
$(template).pluginName({
content: '确定删除该地址'
}).show();
更多可参考