jQuery UI 对话框 closeOnEscape 不适用于多个打开的对话框

发布于 2024-11-06 13:47:07 字数 986 浏览 1 评论 0原文

我的场景:

  1. 当单击 Dialog1 中的特定元素时,Dialog2 将打开。

  2. 当您按 Escape 关闭 Dialog2 时,将按预期工作并关闭 Dialog2。

  3. Dialog1 仍然存在,您可能会认为可以通过再次按 Escape 来关闭它,但事实并非如此。您必须先单击该对话框,然后按 Escape 才能将其关闭。

这是我尝试过的方法,但没有成功:

// Array to hold all of our open dialogs id's
var openDialogs = [];

// the open: method in my dialog
open: function() {

    openDialogs[openDialogs.length] = $(this).attr("id");

}

// the close: method in my dialog
close: function() {

    var index = $.inArray($(this).attr("id"), openDialogs),
    $previousDialog = (index > 0) ? $("#" + openDialogs[index]) : undefined;

    // remove the current dialog from the array via Jon Resig's remove() method
    openDialogs.remove(index);          

    // set focus to previously opened dialog
    if ($previousDialog) { $previousDialog.focus(); }

    // I've even tried:
    //
    // if ($previousDialog) { $previousDialog.dialog("moveToTop"); }

}

My scenario:

  1. When a particular element in Dialog1 is clicked, Dialog2 opens.

  2. When you hit Escape to close Dialog2, works as expected and closes Dialog2.

  3. Dialog1 remains and you would think that it could be closed by hitting Escape again, but it doesn't. You have to first click on the dialog and then hit Escape for it to close.

Here's what I have tried, but to no avail:

// Array to hold all of our open dialogs id's
var openDialogs = [];

// the open: method in my dialog
open: function() {

    openDialogs[openDialogs.length] = $(this).attr("id");

}

// the close: method in my dialog
close: function() {

    var index = $.inArray($(this).attr("id"), openDialogs),
    $previousDialog = (index > 0) ? $("#" + openDialogs[index]) : undefined;

    // remove the current dialog from the array via Jon Resig's remove() method
    openDialogs.remove(index);          

    // set focus to previously opened dialog
    if ($previousDialog) { $previousDialog.focus(); }

    // I've even tried:
    //
    // if ($previousDialog) { $previousDialog.dialog("moveToTop"); }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2024-11-13 13:47:07

这一行:

var index = $.inArray($(this).attr("id"), openDialogs),
$previousDialog = (index > 0) ? $("#" + openDialogs[index]) : undefined;

将使 $previousDialog 引用当前对话框(正在关闭的对话框)。 index 是正在关闭的对话框的索引。

因此,稍后在这一行:

if ($previousDialog) { $previousDialog.focus(); }

在关闭的对话框上调用 focus()

我相信你想要的是这样的:

close: function() {
    var index = $.inArray($(this).attr("id"), openDialogs),
        $previousDialog = (index - 1 >= 0) ? $("#" + openDialogs[index - 1]) : undefined;

    // remove the current dialog from the array via Jon Resig's remove() method
    openDialogs.remove(index);

    // set focus to previously opened dialog
    if ($previousDialog) {
        $previousDialog.focus();
    }
}

这是一个工作示例: http://jsfiddle.net/L8J7Y/

我还注意到,如果您使用 $previousDialog.dialog("widget").focus(); 相反,您可以避免在对话框周围出现可能令人讨厌的虚线。

This line:

var index = $.inArray($(this).attr("id"), openDialogs),
$previousDialog = (index > 0) ? $("#" + openDialogs[index]) : undefined;

Will make $previousDialog refer to the current dialog (the one being closed). index is the index of the dialog that is being closed.

Therefore, later on this line:

if ($previousDialog) { $previousDialog.focus(); }

Is calling focus() on the dialog that was closed.

I believe what you want is something like this:

close: function() {
    var index = $.inArray($(this).attr("id"), openDialogs),
        $previousDialog = (index - 1 >= 0) ? $("#" + openDialogs[index - 1]) : undefined;

    // remove the current dialog from the array via Jon Resig's remove() method
    openDialogs.remove(index);

    // set focus to previously opened dialog
    if ($previousDialog) {
        $previousDialog.focus();
    }
}

Here's a working example: http://jsfiddle.net/L8J7Y/

I also noticed that if you use $previousDialog.dialog("widget").focus(); instead, you avoid getting the possibly annoying dotted line around the dialog.

世界如花海般美丽 2024-11-13 13:47:07

在关闭函数中如何将其放入,

 $(".ui-dialog-content").dialog("close");

因为所有对话框都具有该类,因此它将关闭所有对话框

how about in the close function you put this

 $(".ui-dialog-content").dialog("close");

as all dialogs have that class so it will close all dialogs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文