简体   繁体   English

jQuery UI模态对话框全局单击覆盖关闭

[英]jQuery UI modal dialog global close on overlay click

I have several modal dialogs in my page and there will be even more. 我的页面上有几个模式对话框,将来还会有更多。 I plan to use modal dialogs for them all and have them close on overlay click. 我计划对它们全部使用模式对话框,并在单击叠加时关闭它们。 I don't want to bind the overlay click event in every place I instantiate a dialog, so I would like to extend the dialog prototype or something similar to make it bind the click event globally for all modal dialogs. 我不想在实例化对话框的每个地方都绑定overlay click事件,所以我想扩展对话框原型或类似的东西,使其在所有模式对话框中全局绑定click事件。

http://jsfiddle.net/jurchiks/kLBJm/1/ http://jsfiddle.net/jurchiks/kLBJm/1/

Let's say I have the following dialog constructor: 假设我有以下对话框构造函数:

$('#dialog').dialog({
    modal: true,
    open: function()
    {
        $(this).find('input[type=text]').focus();
    }
});

I've tried putting this code before dialog instantiation: 我尝试将这段代码放在对话框实例化之前:

$.extend(
    $.ui.dialog.prototype.options,
    {
        open: function()
        {
            var dialog = this;
            $('.ui-widget-overlay').on('click', function()
            {
                $(dialog).dialog('close');
            });
        }
    }
);

but it does not work, jQuery only calls the open function passed in the dialog parameters. 但它不起作用,jQuery仅调用在对话框参数中传递的open函数。 I have suspicion that this simply changes the default open function and whatever is passed to the constructor is overriding this. 我怀疑这只会更改默认的打开函数,而传递给构造函数的任何内容都将覆盖此函数。

$.widget(
    "ui.dialog",
    $.ui.dialog,
    {
        open: function()
        {
            this._super();
            var dialog = this;
            $('.ui-widget-overlay').on('click', function()
            {
                $(dialog).dialog('close');
            });
        }
    }
);

This does not work either; 这也不起作用; jQuery spits out an error - "cannot call methods on dialog prior to initialization; attempted to call method 'close'" - even though the popup is open. jQuery吐出一个错误-“在初始化之前无法在对话框上调用方法;​​试图调用方法'close'”-即使弹出窗口处于打开状态。 How could I make it so the overlay click & close event is global and not overridable? 我该如何做才能使覆盖点击和关闭事件是全局且不可替代的?

PS why the hell is this not provided by the jQuery UI library? PS为什么jQuery UI库没有提供此功能? I would think this is something very popular. 我认为这很受欢迎。

Ok, I made my own slightly hackish solution that works, but it seems that it only works on my version of jQuery (1.10.3). 好的,我制定了自己的略带hackish的解决方案,但似乎只适用于我的jQuery(1.10.3)版本。 I tried the exact same code with jsfiddle's offered jQuery UI 1.9.2 and it simply didn't call the function. 我使用jsfiddle提供的jQuery UI 1.9.2尝试了完全相同的代码,但它只是没有调用该函数。 I'm guessing the function name has been renamed in 1.10. 我猜函数名称已在1.10中重命名。

Anyway, here's the code: 无论如何,这是代码:

$.widget(
    'ui.dialog',
    $.ui.dialog,
    {
        _createOverlay: function()
        {
            this._super();

            if (!this.options.modal)
            {
                return;
            }

            this._on(this.overlay, { click: 'close' });
        }
    }
);

And the test fiddle: http://jsfiddle.net/jurchiks/R944y/1/ 和测试提琴: http : //jsfiddle.net/jurchiks/R944y/1/

Had to add external jQuery UI 1.10.3 CDN links to it to make it work, but hey - at least it works and it's a pretty simple solution, just had to dig inside jQuery UI's code to find out the simplest way to do it. 不得不向其添加外部jQuery UI 1.10.3 CDN链接以使其正常运行,但是-至少它可以正常运行,并且这是一个非常简单的解决方案,只需要深入研究jQuery UI的代码以找出最简单的方法即可。

PS This would take so little effort to be built into the dialog plugin itself, it is remarkable that nobody has done it. PS这将花费很少的精力构建到对话框插件本身中,这是很明显的,没有人做。 All you'd need to do is add an option to enable/disable close on overlay click and the last line of my function inside an IF that checks if that option is enabled, a total of max. 您所需要做的就是添加一个选项,以启用/禁用重叠点击关闭功能;如果要检查该选项是否启用,我的函数的最后一行在IF中,总计为max。 4 lines. 4行。

Edit: created a patch for current jQuery UI: https://gist.github.com/jurchiks/7264596 编辑:为当前的jQuery UI创建了一个补丁: https : //gist.github.com/jurchiks/7264596

it's not overriding but you can listen to certain events on the document: 这不是覆盖,但是您可以听文档上的某些事件:

$(function() {
  var theDialog;
  $( document ).on("click",
    ".ui-widget-overlay",function(){
      $(theDialog).dialog("close");
  });
  $( document ).on( "dialogopen", function( event, ui ) {
    theDialog=$(event.target);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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