如何删除由jQuery UI创建的对话框上的关闭按钮(右上角的X)?
如何删除jQuery UI对话框上的关闭按钮?
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();
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();
You can also remove your header line:
<div data-role="header">...</div>
which removes the close button.
$(".ui-button-icon-only").hide();
For the deactivating the class, the short code:
$(".ui-dialog-titlebar-close").hide();
may be used.
Robert MacLean的答案对我不起作用。
但是,这确实对我有用:
$("#div").dialog({
open: function() { $(".ui-dialog-titlebar-close").hide(); }
});
调用.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>
您可以使用CSS隐藏关闭按钮,而不是JavaScript:
.ui-dialog-titlebar-close{
display: none;
}
如果您不想影响所有模态,则可以使用如下规则
.hide-close-btn .ui-dialog-titlebar-close{
display: none;
}
并应用于.hide-close-btn
对话框的顶部节点
如官方页面所示并由David建议:
创建样式:
.no-close .ui-dialog-titlebar-close {
display: none;
}
然后,您可以简单地将no-close类添加到任何对话框以隐藏其关闭按钮:
$( "#dialog" ).dialog({
dialogClass: "no-close",
buttons: [{
text: "OK",
click: function() {
$( this ).dialog( "close" );
}
}]
});
我发现这最终成功了(请注意,第三行覆盖了打开功能,该功能查找按钮并将其隐藏):
$("#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;
}
How about using this pure CSS line? I find this the cleanest solution for a dialog with given Id: