扩展jquery ui对话框(添加更多选项)

我如何为jQuery对话框创建和添加新选项? 例如:我喜欢通过设置选项可以控制标题栏的显示或显示关闭按钮。

脚本将是这样的:

$("#message").dialog({ showTitle:false, //new option (hide Title bar) showCloseButton:true //new option (show close button) modal:true... //other options }) 

这比我在评论中表达的要容易一些。

 // store old method for later use var oldcr = $.ui.dialog.prototype._create; // add the two new options with default values $.ui.dialog.prototype.options.showTitlebar = true; $.ui.dialog.prototype.options.showClosebutton = true; // override the original _create method $.ui.dialog.prototype._create = function(){ oldcr.apply(this,arguments); if (!this.options.showTitlebar) { this.uiDialogTitlebar.hide(); } else if (!this.options.showClosebutton) { this.uiDialogTitlebar.find(".ui-dialog-titlebar-close").hide(); } }; // this is how you use it $("
").dialog({ showClosebutton: false }); // or $("
").dialog({ showTitlebar: false });

显然,如果标题栏被隐藏,关闭按钮也将被隐藏,因为它是标题栏的一部分。

从jQuery UI 1.9开始,您可以以更好的方式扩展小部件,而无需创建新的小部件。

http://learn.jquery.com/jquery-ui/widget-factory/extending-widgets/

请参阅 – 重新定义小组件。

 $.widget( "ui.dialog", $.ui.dialog, { open: function() { console.log( "open" ); return this._super(); } }); $( "
" ).dialog();