7*4 2009-05-22 07:55 采纳率: 50%
浏览 502
已采纳

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

How do I remove the close button (the X in the top-right corner) on a dialog box created by jQuery UI?

转载于:https://stackoverflow.com/questions/896777/how-to-remove-close-button-on-the-jquery-ui-dialog

  • 写回答

22条回答 默认 最新

  • from.. 2009-05-22 11:20
    关注

    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:

    .ui-dialog-titlebar-close {
        visibility: hidden;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 笑故挽风 2009-05-22 07:58
    关注

    You can use CSS to hide the close button instead of JavaScript:

    .ui-dialog-titlebar-close{
        display: none;
    }
    
    评论
  • ℙℕℤℝ 2009-11-26 10:38
    关注

    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();
    },
    
    评论
  • Memor.の 2009-12-10 09:59
    关注
    open: function(event, ui) { 
      //hide close button.
      $(this).parent().children().children('.ui-dialog-titlebar-close').click(function(){
        $("#dhx_combo_list").remove();
      });
    },
    

    yaaaay! It's really working! I catch the close event of the dialog box. In the above code, it removes the div (#dhx_combo_list).

    Great, thanks you all!

    评论
  • YaoRaoLov 2010-05-13 11:26
    关注
    $("#div2").dialog({
       closeOnEscape: false,
       open: function(event, ui) { $('#div2').parent().find('a.ui-dialog-titlebar-close').hide();}
    });
    
    评论
  • 游.程 2010-11-26 19:04
    关注

    I think this is better.

    open: function(event, ui) {
      $(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').hide();
    }
    
    评论
  • ℙℕℤℝ 2011-02-02 16:45
    关注

    The best way to hide the button is to filter it with it's data-icon attribute:

    $('#dialog-id [data-icon="delete"]').hide();
    
    评论
  • ~Onlooker 2011-03-14 16:10
    关注

    Here is another option just using CSS that does not over ride every dialog on the page.

    The CSS

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

    The HTML

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

    The Javascript.

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

    Working Example

    评论
  • local-host 2011-03-15 01:15
    关注

    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
    
    评论
  • bug^君 2011-10-19 12:46
    关注

    For the deactivating the class, the short code:

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

    may be used.

    评论
  • hurriedly% 2012-02-20 01:40
    关注

    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.

    评论
  • 乱世@小熊 2012-04-08 12:23
    关注

    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:

    $("#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:

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

    评论
  • ?yb? 2012-04-12 08:17
    关注

    Robert MacLean's answer did not work for me.

    This however does work for me:

    $("#div").dialog({
       open: function() { $(".ui-dialog-titlebar-close").hide(); }
    });
    
    评论
  • 喵-见缝插针 2012-11-21 05:18
    关注

    You can also remove your header line:

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

    which removes the close button.

    评论
  • 七度&光 2012-12-14 12:09
    关注

    The close button added by the Dialog widget has the class 'ui-dialog-titlebar-close', so after your initial call to .dialog(), you can use a statement like this to remove the close button again: It works..

    $( 'a.ui-dialog-titlebar-close' ).remove();
    
    评论
  • 斗士狗 2013-03-19 12:26
    关注

    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:

    $( "#dialog" ).dialog({
        dialogClass: "no-close",
        buttons: [{
            text: "OK",
            click: function() {
                $( this ).dialog( "close" );
            }
        }]
    });
    
    评论
  • 笑故挽风 2013-05-23 12:23
    关注
    $(".ui-button-icon-only").hide();
    
    评论
  • MAO-EYE 2014-10-22 06:37
    关注
    document.querySelector('.ui-dialog-titlebar-close').style.display = 'none'
    
    评论
  • 10.24 2017-03-27 07:49
    关注

    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();
    
    评论
  • 七度&光 2017-09-21 02:29
    关注

    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();
    
    评论
  • 笑故挽风 2018-06-13 11:28
    关注

    Easy way to achieve: (Do this in your 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
        },
        ....
    });
    
    评论
  • 游.程 2018-09-15 16:28
    关注

    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();
    
    评论
查看更多回答(21条)

报告相同问题?

悬赏问题

  • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?
  • ¥15 有人会思科模拟器嘛?
  • ¥30 遇到一个的问题,请教各位
  • ¥20 matlab报错,vflux计算潜流通量
  • ¥15 我该如何实现鼠标按下GUI按钮时就执行按钮里面的操作的方法
  • ¥15 关于#硬件工程#的问题:我这边有个锁相环电路没有效果
  • ¥15 20款 27寸imac苹果一体机装win10后,蓝牙耳机和音响放歌曲卡顿断断续续.
  • ¥15 VB.NET 父窗体调取子窗体报错
  • ¥15 python海龟作图如何改代码使其最后画出来的是一个镜像翻转的图形
  • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?