【问题标题】:How to remove close button on the jQuery UI dialog?如何删除 jQuery UI 对话框上的关闭按钮?
【发布时间】:2010-10-28 03:52:51
【问题描述】:

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

【问题讨论】:

  • 查看文档,第一个副标题:api.jqueryui.com/dialog
  • @MikeCole 该文档适用于 1.10 - 我认为如果您想在任何较低版本中隐藏关闭按钮,您需要执行类似于以下答案的操作。
  • 使用 "ui-dialog-titlebar-close": "display:none;"当我们设置 jqueryui 模态对话框时

标签: jquery html css jquery-ui jquery-ui-dialog


【解决方案1】:

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

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

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

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

【讨论】:

  • $(".ui-dialog-titlebar-close", ui).hide(); 仅隐藏 this 对话框的按钮。
  • 我也无法通过 ui 参数使其工作。我最终使用了: $(".ui-dialog-titlebar-close", $(this).parent()).hide();
  • @Anton 只想指出,仅指定 'ui' 是行不通的。你必须使用'ui.dialog'。所以正确的行是 $(".ui-dialog-titlebar-close", ui.dialog).hide();
  • @Bradley。 ui 对我不起作用, ui.dialog 起作用但适用于每个实例。要将 ot 工作仅应用于定义了 open 函数的那个​​,我必须这样做: $(".ui-dialog-titlebar-close", this.parentNode).hide();
  • open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
【解决方案2】:

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

Working Example

【讨论】:

  • 我喜欢这种方法,因为我可以将它与以下内容一起使用: .noTitleDlg .ui-dialog-titlebar {display:none} 在 CSS 中构建我希望我的对话框出现和行为的方式以及然后相应地设置 dialogClass。
  • 非常干净的解决方案... +1 不涉及额外的 js 功能来删除按钮。
  • 好主意。在您对所有对话框使用相同的打开方法并且针对特定实例更改它不是很实际的情况下,可以方便地定位特定对话框。
  • 我最喜欢的解决方案。我在想这样的事情将是最好的方法。感谢您已经在这里编码。在此基础上,我喜欢使用这种变体,它将获取对话框内容 div 的 class 属性,我可以将“no-close”类放入其中:dialogClass : $("#my-dialog-id").attr("class"),
  • 此解决方案允许使用转义关闭,如果您想防止关闭转义使用:$( ".selector" ).dialog({ dialogClass: 'no-close' , closeOnEscape: false,});
【解决方案3】:

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

open: function(event, ui) { 
    //hide close button.
    $(this).parent().children().children('.ui-dialog-titlebar-close').hide();
},

【讨论】:

  • 这比你需要的要复杂。 $(".ui-dialog-titlebar-close", $(this).parent()).hide();
  • @KevinPanko 在 .aspx 页面中使用带有 ASP.NET v2.0 的 jquery ui 演示站点提供的示例时,您的建议效果很好。 jqueryui.com/demos/dialog/modal-form.html
  • .closest('.ui-dialog') 比假设父母要好。
【解决方案4】:

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

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

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

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

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

【讨论】:

  • 这个答案简单明了。但是,仅当您希望禁用所有对话框的按钮时,它才适用。
  • @MarkBrittingham 您可以将自定义 CSS 类应用于您的模式和选择器,然后这将应用于您想要的任何人
【解决方案5】:

如官方page所示,大卫建议:

创建样式:

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

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

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

【讨论】:

    【解决方案6】:

    我觉得这样更好。

    open: function(event, ui) {
      $(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').hide();
    }
    

    【讨论】:

    • 问题是它偶尔也会隐藏其他对话的关闭按钮。如何防止这种情况。
    • 即使使用 open: function(event, ui) { $(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').show( ); } 不起作用。
    【解决方案7】:

    在元素上调用 .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>
    

    Demos here

    【讨论】:

    • 太棒了!!这允许在对话框初始化后显示/隐藏关闭按钮。
    【解决方案8】:

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

    这对我有用:

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

    【讨论】:

      【解决方案9】:
      $("#div2").dialog({
         closeOnEscape: false,
         open: function(event, ui) { $('#div2').parent().find('a.ui-dialog-titlebar-close').hide();}
      });
      

      【讨论】:

        【解决方案10】:

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

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

        请检查它是否适合您。

        【讨论】:

          【解决方案11】:

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

          $('#dialog-id [data-icon="delete"]').hide();
          

          【讨论】:

            【解决方案12】:

            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
            

            【讨论】:

              【解决方案13】:

              对于停用类,短代码:

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

              可以使用。

              【讨论】:

                【解决方案14】:

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

                $( 'a.ui-dialog-titlebar-close' ).remove();
                

                【讨论】:

                  【解决方案15】:

                  我捕捉到对话框的关闭事件。然后这段代码删除了&lt;div&gt; (#dhx_combo_list):

                  open: function(event, ui) { 
                    //hide close button.
                    $(this).parent().children().children('.ui-dialog-titlebar-close').click(function(){
                      $("#dhx_combo_list").remove();
                    });
                  },
                  

                  【讨论】:

                    【解决方案16】:
                    $(".ui-button-icon-only").hide();
                    

                    【讨论】:

                    • 如果你只是隐藏图标,你应该可以在这里直接使用 CSS 而不是 JS。所有.hide() 所做的都是在CSS 中设置display:none,所以$(".ui-button-icon-only").hide(); 在功能上等同于.ui-button-icon-only { display: none; }
                    【解决方案17】:

                    您也可以删除标题行:

                    &lt;div data-role="header"&gt;...&lt;/div&gt;

                    移除关闭按钮。

                    【讨论】:

                      【解决方案18】:

                      实现的简单方法:(在您的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
                          },
                          ....
                      });
                      

                      【讨论】:

                        【解决方案19】:
                        document.querySelector('.ui-dialog-titlebar-close').style.display = 'none'
                        

                        【讨论】:

                          【解决方案20】:

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

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

                          【讨论】:

                            【解决方案21】:

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

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

                            【讨论】:

                              【解决方案22】:

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

                              .ui-dialog[aria-describedby="IdValueOfDialog"] .ui-dialog-titlebar-close { display: none; }
                              

                              【讨论】:

                                【解决方案23】:

                                这是针对 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”基于帖子How to implement “confirmation” dialog in Jquery UI dialog?上的“Whome”回答

                                【讨论】:

                                  【解决方案24】:

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

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

                                  【讨论】:

                                    【解决方案25】:

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

                                    $('#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();
                                    

                                    【讨论】:

                                      猜你喜欢
                                      • 2016-04-13
                                      • 2011-04-03
                                      • 2013-03-22
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 2014-08-02
                                      • 1970-01-01
                                      • 1970-01-01
                                      相关资源
                                      最近更新 更多