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

ton*_*007 3 jquery dialog add options jquery-ui-dialog

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

脚本将是这样的:

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

rom*_*nsh 8

从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();
    }
});

$( "<div>" ).dialog();
Run Code Online (Sandbox Code Playgroud)


use*_*654 6

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

// 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
$("<div />").dialog({
    showClosebutton: false
});
// or
$("<div />").dialog({
    showTitlebar: false
});
Run Code Online (Sandbox Code Playgroud)

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