如何在jQuery UI对话框中移除关闭按钮?

809

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


13
查看文档,第一个子标题:http://api.jqueryui.com/dialog/ - Mike Cole
1
@MikeCole 文档是针对1.10版本的 - 我认为如果你想在任何更低的版本中隐藏关闭按钮,你需要做类似下面回答中提到的事情。 - Jarrett
1
在设置jQueryUI模态对话框时,请使用“ui-dialog-titlebar-close”:“display:none;”。 - MarcoZen
25个回答

724

最终我发现这样可行(请注意第三行覆盖了查找按钮并隐藏它的 open 函数):

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

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

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

123
$(".ui-dialog-titlebar-close", ui).hide(); 是用来隐藏这个对话框按钮的代码。 - Anthony Serdyukov
71
我也无法从UI参数中使其运作。最后我使用了以下代码:$(".ui-dialog-titlebar-close", $(this).parent()).hide(); - Nigel
72
@Anton 只是想指出,仅指定“ui”是不起作用的。你必须使用“ui.dialog”。因此,正确的行应该是$(".ui-dialog-titlebar-close", ui.dialog).hide(); - Bradley Mountford
22
@Bradley. ui对我无效,但ui.dialog可以使用,但是会应用于每个实例。为了让它只在定义open函数的那个实例上运行,我必须这样做:$(".ui-dialog-titlebar-close", this.parentNode).hide(); - Nabab
14
open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); } - opengrid
显示剩余16条评论

372

以下是另一种只使用 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' });

演示示例


6
我喜欢这种方法,因为我可以与CSS中的类似代码一起使用,例如:.noTitleDlg .ui-dialog-titlebar {display:none},以构建我想要的对话框外观和行为方式,然后只需相应地设置对话框类。 - A. Murray
11
非常干净的解决方案……因为不涉及使用额外的 JavaScript 功能来移除按钮,所以加一分。 - Bongs
2
好主意。在使用相同的打开方法打开所有对话框的情况下,它非常方便地针对特定对话框进行定位,并且更改特定实例并不是非常实用。 - ZolaKt
3
我最喜欢的解决方案是这样的。我认为这样做是最好的方法。感谢您已经在此处编写好它。在此基础上,我想使用这种变体,它将获取对话框内容div的类属性,其中我可以放置“no-close”类:dialogClass: $(“#my-dialog-id”).attr(“class”), - Mr. Lance E Sloan
1
此解决方案允许使用Escape键关闭,如果您想要防止使用Escape键关闭,请使用以下代码: $( ".selector" ).dialog({ dialogClass: 'no-close' , closeOnEscape: false,}); - Mladen Adamovic

128

对于多个对话框,“最佳”答案可能并不好。这里有一个更好的解决方案。

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

24
这比你需要的要复杂。$(".ui-dialog-titlebar-close", $(this).parent()).hide(); - Kevin Panko
@KevinPanko你的建议在使用jquery ui演示站点提供的示例与ASP.NET v2.0在.aspx页面中一起使用时效果很好。http://jqueryui.com/demos/dialog/modal-form.html - Matthew Dally
6
.closest('.ui-dialog')比假设父元素更好。 - technomage

90
你可以使用CSS来隐藏关闭按钮,而不是使用JavaScript:
.ui-dialog-titlebar-close{
    display: none;
}

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

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

.hide-close-btn应用于对话框的顶级节点。


3
这个答案简单明了。但需要注意的是,它仅适用于您希望禁用所有对话框中的按钮。Translated: 这个答案简单明了。但需要注意的是,它仅适用于您希望禁用所有对话框中的按钮。 - Mark Brittingham
@MarkBrittingham 你可以为你的模态框和选择器应用自定义CSS类,然后这将适用于你想要的任何人。 - Ruan Mendes

54

正如官方页面所示,并建议由David提供:

创建一个样式:

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

然后,您只需将“no-close”类添加到任何对话框中,以隐藏其关闭按钮:

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

42

我认为这样更好。

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

问题是,它偶尔会隐藏其他对话框的关闭按钮。如何防止这种情况发生。 - Zaveed Abbasi
即使使用以下代码: open: function(event, ui) { $(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').show(); } 仍然无法正常工作。 - Zaveed Abbasi

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指的是被“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>

这里有演示


太好了!这允许在对话框已经初始化之后显示/隐藏关闭按钮。 - kRiZ

29

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

然而,下面这个对我有效:

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

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

请检查它是否对您有效。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接