[英]Best way to remove the close button on jQuery UI dialog box widget?
[英]How to remove close button on the jQuery UI dialog?
如何刪除 jQuery UI 創建的對話框上的關閉按鈕(右上角的X )?
我發現這最終有效(注意第三行覆蓋了找到按鈕並隱藏它的打開函數):
$("#div2").dialog({
closeOnEscape: false,
open: function(event, ui) {
$(".ui-dialog-titlebar-close", ui.dialog || ui).hide();
}
});
要隱藏所有對話框上的關閉按鈕,您也可以使用以下 CSS:
.ui-dialog-titlebar-close {
visibility: hidden;
}
這是僅使用 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' });
“最佳”答案不適用於多個對話。 這是一個更好的解決方案。
open: function(event, ui) {
//hide close button.
$(this).parent().children().children('.ui-dialog-titlebar-close').hide();
},
您可以使用 CSS 來隱藏關閉按鈕而不是 JavaScript:
.ui-dialog-titlebar-close{
display: none;
}
如果你不想影響所有的模態,你可以使用像這樣的規則
.hide-close-btn .ui-dialog-titlebar-close{
display: none;
}
並將.hide-close-btn
到對話框的頂部節點
如官方頁面所示並由大衛建議:
創建樣式:
.no-close .ui-dialog-titlebar-close {
display: none;
}
然后,您可以簡單地將 no-close 類添加到任何對話框以隱藏它的關閉按鈕:
$( "#dialog" ).dialog({
dialogClass: "no-close",
buttons: [{
text: "OK",
click: function() {
$( this ).dialog( "close" );
}
}]
});
我覺得這樣更好。
open: function(event, ui) {
$(this).closest('.ui-dialog').find('.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>
羅伯特麥克萊恩的回答對我不起作用。
然而,這對我有用:
$("#div").dialog({
open: function() { $(".ui-dialog-titlebar-close").hide(); }
});
$("#div2").dialog({
closeOnEscape: false,
open: function(event, ui) { $('#div2').parent().find('a.ui-dialog-titlebar-close').hide();}
});
以上都不起作用。 真正有效的解決方案是:
$(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
})
請檢查它是否適合您。
隱藏按鈕的最佳方法是使用它的 data-icon 屬性過濾它:
$('#dialog-id [data-icon="delete"]').hide();
Dialog 小部件添加的關閉按鈕具有“ui-dialog-titlebar-close”類,因此在您初始調用 .dialog() 后,您可以使用這樣的語句再次刪除關閉按鈕:它有效..
$( 'a.ui-dialog-titlebar-close' ).remove();
我捕獲了對話框的關閉事件。 此代碼然后刪除<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();
});
},
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
對於停用類,短代碼:
$(".ui-dialog-titlebar-close").hide();
可能用過了。
$(".ui-button-icon-only").hide();
您還可以刪除標題行:
<div data-role="header">...</div>
它刪除了關閉按鈕。
document.querySelector('.ui-dialog-titlebar-close').style.display = 'none'
實現的簡單方法:(在您的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
},
....
});
因為我發現我在我的應用程序的幾個地方都這樣做了,所以我把它包裝在一個插件中:
(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();
我是單線的粉絲(他們在哪里工作!)。 以下是對我有用的方法:
$("#dialog").siblings(".ui-dialog-titlebar").find(".ui-dialog-titlebar-close").hide();
使用這個純 CSS 行怎么樣? 我發現這是具有給定 Id 的對話的最干凈的解決方案:
.ui-dialog[aria-describedby="IdValueOfDialog"] .ui-dialog-titlebar-close { display: none; }
這適用於 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 對話框中實現“確認”對話框?
對於那些使用誰是DialogExtend jQuery Extension
,可以使用closable
選項來管理此功能以及許多其他的調整,這種體面的擴展提供。
請注意,如果您已經在使用DialogExtend
,則上述任何 Dialog CSS hacks 在初始化時都會被DialogExtend
破壞。
您可以使用以下代碼刪除關閉按鈕。 還有其他選項可能對您有用。
$('#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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.