简体   繁体   English

如何删除 jQuery UI 对话框上的关闭按钮?

[英]How to remove close button on the jQuery UI dialog?

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

I have found this worked in the end (note the third line overriding the open function which find the button and hides it):我发现这最终有效(注意第三行覆盖了找到按钮并隐藏它的打开函数):

$("#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:要隐藏所有对话框上的关闭按钮,您也可以使用以下 CSS:

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

Here is another option just using CSS that does not over ride every dialog on the page.这是仅使用 CSS 的另一种选择,它不会覆盖页面上的每个对话框。

The CSS CSS

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

The HTML HTML

<div class="selector" title="No close button">
    This is a test without a close button
</div>

The Javascript. Javascript。

$( ".selector" ).dialog({ dialogClass: 'no-close' });

Working Example工作示例

the "best" answer will not be good for multiple dialogs. “最佳”答案不适用于多个对话。 here is a better solution.这是一个更好的解决方案。

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

You can use CSS to hide the close button instead of JavaScript:您可以使用 CSS 来隐藏关闭按钮而不是 JavaScript:

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

If you don't want to affect all the modals, you could use a rule like如果你不想影响所有的模态,你可以使用像这样的规则

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

And apply .hide-close-btn to the top node of the dialog并将.hide-close-btn到对话框的顶部节点

As shown on the official page and suggested by David:如官方页面所示并由大卫建议:

Create a style:创建样式:

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

Then, you can simply add the no-close class to any dialog in order to hide it's close button:然后,您可以简单地将 no-close 类添加到任何对话框以隐藏它的关闭按钮:

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

I think this is better.我觉得这样更好。

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

Once you have called .dialog() on an element, you can locate the close button (and other dialog markup) at any convenient time without using event handlers:在元素上调用.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

Alternate method:替代方法:

Inside dialog event handlers, this refers to the element being "dialogged" and $(this).parent() refers to the dialog markup container, so:在对话框事件处理程序中, 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
    }
});

FYI, dialog markup looks like this:仅供参考,对话框标记如下所示:

<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演示在这里

Robert MacLean's answer did not work for me.罗伯特麦克莱恩的回答对我不起作用。

This however does work for me:然而,这对我有用:

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

None of the above works.以上都不起作用。 The solution that really works is:真正有效的解决方案是:

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

Please check if it works for you.请检查它是否适合您。

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

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

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

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

I catch the close event of the dialog box.我捕获了对话框的关闭事件。 This code then removes the <div> ( #dhx_combo_list ):此代码然后删除<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/ 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

For the deactivating the class, the short code:对于停用类,短代码:

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

may be used.可能用过了。

$(".ui-button-icon-only").hide();

You can also remove your header line:您还可以删除标题行:

<div data-role="header">...</div>

which removes the close button.它删除了关闭按钮。

document.querySelector('.ui-dialog-titlebar-close').style.display = 'none'

Easy way to achieve: (Do this in your Javascript )实现的简单方法:(在您的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
    },
    ....
});

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

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

How about using this pure CSS line?使用这个纯 CSS 行怎么样? I find this the cleanest solution for a dialog with given Id:我发现这是具有给定 Id 的对话的最干净的解决方案:

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

This is for jQuery UI 1.12.这适用于 jQuery UI 1.12。 I added the following configuration setting for 'classes' option我为“类”选项添加了以下配置设置

        classes: {
            'ui-dialog-titlebar-close': 'hidden',
        },

the whole dialog initialization looks like this:整个对话框初始化如下所示:

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

I use following script call to show it:我使用以下脚本调用来显示它:

ConfirmarSiNo('Titulo',
              '¿Desea actualizar?',
              idModalPadre,
              (eligioSi) => {
                            if (eligioSi) {
                                this.$tarifa.val(tarifa.tarifa);
                                this.__ActualizarTarifa(tarifa);
                            }
                        });

whithin Html body I have the following div that contains the dialog:在 Html 正文中,我有以下包含对话框的 div:

<div class="modal" id="confirmar-si-no" title="" aria-labelledby="confirmacion-label">
    mensaje
</div>

final result is:最终结果是:

在此处输入图片说明

function 'ConfirmarSiNo' is based on ' Whome ' answer on post How to implement “confirmation” dialog in Jquery UI dialog?函数 'ConfirmarSiNo' 基于帖子中的 ' Whome ' 答案如何在 Jquery UI 对话框中实现“确认”对话框?

For those who are using DialogExtend jQuery Extension , you can use the closable option to manage this feature as well as many other tweaks that this decent extension provides.对于那些使用谁是DialogExtend jQuery Extension ,可以使用closable选项来管理此功能以及许多其他的调整,这种体面的扩展提供。

Note, if you are already using DialogExtend , any of the aforementioned Dialog CSS hacks will get clobbered by DialogExtend when initialized.请注意,如果您已经在使用DialogExtend ,则上述任何 Dialog CSS hacks 在初始化时都会被DialogExtend破坏。

You can remove the close button with the code below.您可以使用以下代码删除关闭按钮。 There are other options as well which you might fight useful.还有其他选项可能对您有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM