jQuery-UI close modal dialog on background click

后端 未结 5 2257
忘掉有多难
忘掉有多难 2021-02-14 16:09

I\'m toying with jQuery UI, and I like how this demo works: http://jqueryui.com/demos/dialog/#modal-message

However, when a dialog comes up, the only way to close it is

相关标签:
5条回答
  • 2021-02-14 16:13

    That should do the trick:

    $(".ui-widget-overlay").click(function(){
        $(".ui-dialog-titlebar-close").trigger('click');
    });
    

    Click on .ui-widget-overlay will trigger the click on the close button

    Cheers

    G.

    0 讨论(0)
  • 2021-02-14 16:17

    just to add in case anyone run's into this problem - if you have multiple dialogs stacked on top of each other then the following will close just the dialog that's at the top:

    $(".ui-widget-overlay").live("click", function () {
                $(".ui-dialog-titlebar-close", $(this).prev()).trigger('click');
            });
    
    0 讨论(0)
  • 2021-02-14 16:19

    I've found the previous to be finicky at times, here's a simple fix:

    $(".ui-widget-overlay").live('click', function(){
       $(".ui-dialog-titlebar-close").trigger('click');
    });
    
    0 讨论(0)
  • 2021-02-14 16:28

    This one will definitely work, since it matters when the overlay is in the dom or not.

    $(document).on('click', '.ui-widget-overlay', function(){
      $(".ui-dialog-titlebar-close").trigger('click');
    });
    
    0 讨论(0)
  • 2021-02-14 16:30

    This is the preferred method to use when dealing with newer versions of Jquery.

    $(".ui-widget-overlay").on("click", function(){
        $(".ui-dialog-titlebar-close").trigger('click');
    });
    
    0 讨论(0)
提交回复
热议问题