如何删除jQueryUI创建的对话框上的关闭按钮(右上角的X)?
当前回答
如官方页面所示,David建议:
创建样式:
.no-close .ui-dialog-titlebar-close {
display: none;
}
然后,您可以简单地将no close类添加到任何对话框中,以隐藏其关闭按钮:
$( "#dialog" ).dialog({
dialogClass: "no-close",
buttons: [{
text: "OK",
click: function() {
$( this ).dialog( "close" );
}
}]
});
其他回答
您可以使用以下代码删除关闭按钮。还有其他的选择,你可能会觉得有用。
$('#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();
这里还有一个选项,就是使用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' });
工作示例
由于我发现我在应用程序的几个地方都在做这件事,所以我把它包装在一个插件中:
(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();
对于多个对话框,“最佳”答案并不好。这里有一个更好的解决方案。
open: function(event, ui) {
//hide close button.
$(this).parent().children().children('.ui-dialog-titlebar-close').hide();
},
对于停用类,短代码:
$(".ui-dialog-titlebar-close").hide();
可以使用。