Click outside non-modal dialog to close

前端 未结 1 1693
青春惊慌失措
青春惊慌失措 2020-12-16 05:22

Per my previous research, I\'ve been able to figure out how to trigger a live click event on the overlay around a dialog to close the dialog. However, that restricts furthe

相关标签:
1条回答
  • 2020-12-16 05:51

    Finally figured out the answer to my own question. By binding a mousedown event to the document itself and then excluding the dialog, we can duplicate the functionality of the live function for overlays. Below the code I've included a jsFiddle demonstrating the solution.

    // Listen for document click to close non-modal dialog
    $(document).mousedown(function(e) {
        var clicked = $(e.target); // get the element clicked
        if (clicked.is('#dlg') || clicked.parents().is('#dlg') || clicked.is('.ui-dialog-titlebar')) {
            return; // click happened within the dialog, do nothing here
        } else { // click was outside the dialog, so close it
            $('#dlg').dialog("close");
        }
    });
    

    http://jsfiddle.net/elwayman02/Z5KA2/

    0 讨论(0)
提交回复
热议问题