800

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

4

25 回答 25

721

我发现这最终奏效了(注意第三行覆盖了找到按钮并将其隐藏的打开函数):

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

要隐藏所有对话框上的关闭按钮,您也可以使用以下 CSS:

.ui-dialog-titlebar-close {
    visibility: hidden;
}
于 2009-05-22T11:20:08.167 回答
367

这是另一个仅使用 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' });

工作示例

于 2011-03-14T16:10:46.413 回答
126

“最佳”答案不适用于多个对话。这是一个更好的解决方案。

open: function(event, ui) { 
    //hide close button.
    $(this).parent().children().children('.ui-dialog-titlebar-close').hide();
},
于 2009-11-26T10:38:21.720 回答
89

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

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

如果您不想影响所有模式,您可以使用类似的规则

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

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

于 2009-05-22T07:58:52.143 回答
51

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

创建样式:

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

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

$( "#dialog" ).dialog({
    dialogClass: "no-close",
    buttons: [{
        text: "OK",
        click: function() {
            $( this ).dialog( "close" );
        }
    }]
});
于 2013-03-19T12:26:42.760 回答
42

我认为这更好。

open: function(event, ui) {
  $(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').hide();
}
于 2010-11-26T19:04:30.877 回答
36

一旦你调用.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>

演示在这里

于 2012-04-08T12:23:37.247 回答
27

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

然而,这对我有用:

$("#div").dialog({
   open: function() { $(".ui-dialog-titlebar-close").hide(); }
});
于 2012-04-12T08:17:45.677 回答
9
$("#div2").dialog({
   closeOnEscape: false,
   open: function(event, ui) { $('#div2').parent().find('a.ui-dialog-titlebar-close').hide();}
});
于 2010-05-13T11:26:14.600 回答
9

以上都不起作用。真正有效的解决方案是:

$(function(){
  //this is your dialog:
  $('#mydiv').dialog({
    // Step 1. Add an extra class to our dialog to address the dialog directly. Make sure that this class is not used anywhere else:
    dialogClass: 'my-extra-class' 
  })
  // Step 2. Hide the close 'X' button on the dialog that you marked with your extra class
  $('.my-extra-class').find('.ui-dialog-titlebar-close').css('display','none');
  // Step 3. Enjoy your dialog without the 'X' link
})

请检查它是否适合您。

于 2012-02-20T01:40:54.370 回答
7

隐藏按钮的最佳方法是使用它的 data-icon 属性对其进行过滤:

$('#dialog-id [data-icon="delete"]').hide();
于 2011-02-02T16:45:12.647 回答
6

我捕捉到对话框的关闭事件。然后,此代码删除<div>( #dhx_combo_list):

open: function(event, ui) { 
  //hide close button.
  $(this).parent().children().children('.ui-dialog-titlebar-close').click(function(){
    $("#dhx_combo_list").remove();
  });
},
于 2009-12-10T09:59:57.707 回答
6

http://jsfiddle.net/marcosfromero/aWyNn/

$('#yourdiv').                 // Get your box ...
  dialog().                    // ... and turn it into dialog (autoOpen: false also works)
  prev('.ui-dialog-titlebar'). // Get title bar,...
  find('a').                   // ... then get the X close button ...
  hide();                      // ... and hide it
于 2011-03-15T01:15:28.587 回答
6

对于停用课程,短代码:

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

可能用过了。

于 2011-10-19T12:46:47.893 回答
6

Dialog 小部件添加的关闭按钮具有类 'ui-dialog-titlebar-close',因此在您初次调用 .dialog() 之后,您可以使用这样的语句再次删除关闭按钮:它可以工作..

$( 'a.ui-dialog-titlebar-close' ).remove();
于 2012-12-14T12:09:25.103 回答
5
$(".ui-button-icon-only").hide();
于 2013-05-23T12:23:05.767 回答
3

您还可以删除标题行:

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

删除关闭按钮。

于 2012-11-21T05:18:51.490 回答
3

实现的简单方法:(在您的 中执行此操作Javascript

$("selector").dialog({
    autoOpen: false,
    open: function(event, ui) {   // It'll hide Close button
        $(".ui-dialog-titlebar-close", ui.dialog | ui).hide();
    },
    closeOnEscape: false,        // Do not close dialog on press Esc button
    show: {
        effect: "clip",
        duration: 500
    },
    hide: {
        effect: "blind",
        duration: 200
    },
    ....
});
于 2018-06-13T11:28:55.790 回答
2
document.querySelector('.ui-dialog-titlebar-close').style.display = 'none'
于 2014-10-22T06:37:29.597 回答
1

因为我发现我在我的应用程序的几个地方都这样做了,所以我将它包装在一个插件中:

(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)

使用示例:

$("#dialog").dialog({ /* lots of options */ }).dialogNoClose();
于 2017-09-21T02:29:31.517 回答
1

我是单线的粉丝(他们工作的地方!)。这对我有用:

$("#dialog").siblings(".ui-dialog-titlebar").find(".ui-dialog-titlebar-close").hide();
于 2018-09-15T16:28:44.077 回答
0

使用这个纯 CSS 行怎么样?对于具有给定 ID 的对话框,我发现这是最干净的解决方案:

.ui-dialog[aria-describedby="IdValueOfDialog"] .ui-dialog-titlebar-close { display: none; }
于 2019-04-12T08:39:10.513 回答
0

这适用于 jQuery UI 1.12。我为“类”选项添加了以下配置设置

        classes: {
            'ui-dialog-titlebar-close': 'hidden',
        },

整个对话框初始化如下所示:

ConfirmarSiNo(titulo, texto, idPadre, fnCerrar) {
    const DATA_RETORNO = 'retval';
    $('confirmar-si-no').dialog({
        title: titulo,
        modal: true,
        classes: {
            'ui-dialog-titlebar-close': 'hidden',
        },
        appendTo: `#${idPadre}`,
        open: function fnOpen() { $(this).text(texto); },
        close: function fnClose() {
            let eligioSi = $(this).data(DATA_RETORNO) == true;
            setTimeout(function () { fnCerrar(eligioSi); }, 30);
        },
        buttons: {
            'Si, por favor': function si() { $(this).data(DATA_RETORNO, true); $(this).dialog("close"); },
            'No, gracias': function no() { $(this).data(DATA_RETORNO, false); $(this).dialog("close"); }
        }
    });
}

我使用以下脚本调用来显示它:

ConfirmarSiNo('Titulo',
              '¿Desea actualizar?',
              idModalPadre,
              (eligioSi) => {
                            if (eligioSi) {
                                this.$tarifa.val(tarifa.tarifa);
                                this.__ActualizarTarifa(tarifa);
                            }
                        });

在 Html 正文中,我有以下包含对话框的 div:

<div class="modal" id="confirmar-si-no" title="" aria-labelledby="confirmacion-label">
    mensaje
</div>

最终结果是:

在此处输入图像描述

功能“ConfirmarSiNo”基于帖子上的“ Whome ”答案如何在 Jquery UI 对话框中实现“确认”对话框?

于 2020-12-08T20:23:13.943 回答
0

对于使用DialogExtend jQuery Extension的用户,您可以使用该closable选项来管理此功能以及此不错的扩展提供的许多其他调整。

请注意,如果您已经在使用DialogExtend,则任何上述 Dialog CSS hackDialogExtend在初始化时都会被破坏。

于 2021-07-11T20:57:11.510 回答
-1

您可以使用以下代码删除关闭按钮。还有其他选项可能对您有用。

$('#dialog-modal').dialog({
    //To hide the Close 'X' button
    "closeX": false,
    //To disable closing the pop up on escape
    "closeOnEscape": false,
    //To allow background scrolling
    "allowScrolling": true
    })
//To remove the whole title bar
.siblings('.ui-dialog-titlebar').remove();
于 2017-03-27T07:49:07.793 回答