如何删除jQueryUI创建的对话框上的关闭按钮(右上角的X)?


当前回答

一旦对元素调用了.dialog(),就可以在任何方便的时候找到关闭按钮(和其他对话框标记),而无需使用事件处理程序:

$("#div2").dialog({                    // call .dialog method to create the dialog markup
    autoOpen: false
});
$("#div2").dialog("widget")            // get the dialog widget element
    .find(".ui-dialog-titlebar-close") // find the close button for this dialog
    .hide();                           // hide it

替代方法:

在对话框事件处理程序中,这是指“dialogged”元素,$(this).parent()是指对话框标记容器,因此:

$("#div3").dialog({
    open: function() {                         // open event handler
        $(this)                                // the element being dialogged
            .parent()                          // get the dialog widget element
            .find(".ui-dialog-titlebar-close") // find the close button for this dialog
            .hide();                           // hide it
    }
});

仅供参考,对话框标记如下所示:

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable">
    <!-- ^--- this is the dialog widget -->
    <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
        <span class="ui-dialog-title" id="ui-dialog-title-dialog">Dialog title</span>
        <a class="ui-dialog-titlebar-close ui-corner-all" href="#"><span class="ui-icon ui-icon-closethick">close</span></a>
    </div>
    <div id="div2" style="height: 200px; min-height: 200px; width: auto;" class="ui-dialog-content ui-widget-content">
        <!-- ^--- this is the element upon which .dialog() was called -->
    </div>
</div>

此处演示

其他回答

罗伯特·麦克莱恩的回答对我不起作用。

然而,这对我来说确实有效:

$("#div").dialog({
   open: function() { $(".ui-dialog-titlebar-close").hide(); }
});

对于停用类,短代码:

$(".ui-dialog-titlebar-close").hide();

可以使用。

如官方页面所示,David建议:

创建样式:

.no-close .ui-dialog-titlebar-close {
    display: none;
}

然后,您可以简单地将no close类添加到任何对话框中,以隐藏其关闭按钮:

$( "#dialog" ).dialog({
    dialogClass: "no-close",
    buttons: [{
        text: "OK",
        click: function() {
            $( this ).dialog( "close" );
        }
    }]
});

这里还有一个选项,就是使用CSS,它不会覆盖页面上的每个对话框。

CSS

.no-close .ui-dialog-titlebar-close {display: none }

HTML

<div class="selector" title="No close button">
    This is a test without a close button
</div>

Javascript。

$( ".selector" ).dialog({ dialogClass: 'no-close' });

工作示例

$(".ui-button-icon-only").hide();