3

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

脚本将是这样的:

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

2 回答 2

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();
于 2014-01-06T04:13:09.603 回答
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
});

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

于 2012-06-05T18:09:56.213 回答