如何删除jQuery UI对话框上的关闭按钮?

CSS

GO西门LEY

2020-03-16

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

第1689篇《如何删除jQuery UI对话框上的关闭按钮?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

11个回答
GOTom 2020.03.16

How about using this pure CSS line? I find this the cleanest solution for a dialog with given Id:

.ui-dialog[aria-describedby="IdValueOfDialog"] .ui-dialog-titlebar-close { display: none; }
Davaid小卤蛋 2020.03.16

I am a fan of one-liners (where they work!). Here is what works for me:

$("#dialog").siblings(".ui-dialog-titlebar").find(".ui-dialog-titlebar-close").hide();
L卡卡西米亚 2020.03.16

Since I found I was doing this in several places in my app, I wrapped it in a plugin:

(function ($) {
   $.fn.dialogNoClose = function () {
      return this.each(function () {
         // hide the close button and prevent ESC key from closing
         $(this).closest(".ui-dialog").find(".ui-dialog-titlebar-close").hide();
         $(this).dialog("option", "closeOnEscape", false);
      });
   };
})(jQuery)

Usage Example:

$("#dialog").dialog({ /* lots of options */ }).dialogNoClose();
凯乐 2020.03.16

You can also remove your header line:

<div data-role="header">...</div>

which removes the close button.

猿Pro 2020.03.16
$(".ui-button-icon-only").hide();
米亚神乐Pro 2020.03.16

For the deactivating the class, the short code:

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

may be used.

村村卡卡西 2020.03.16

Robert MacLean的答案对我不起作用。

但是,这确实对我有用:

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

调用.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

替代方法:

在对话框事件处理程序中,this指的是“ $(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>

这里的演示

小宇宙老丝 2020.03.16

您可以使用CSS隐藏关闭按钮,而不是JavaScript:

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

如果您不想影响所有模态,则可以使用如下规则

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

并应用于.hide-close-btn对话框的顶部节点

理查德阿飞 2020.03.16

如官方页面所示并由David建议:

创建样式:

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

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

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

我发现这最终成功了(请注意,第三行覆盖了打开功能,该功能查找按钮并将其隐藏):

$("#div2").dialog({
    closeOnEscape: false,
    open: function(event, ui) {
        $(".ui-dialog-titlebar-close", ui.dialog || ui).hide();
    }
});

To hide the close button on all dialogs you can use the following CSS too:

.ui-dialog-titlebar-close {
    visibility: hidden;
}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android