我的情景:
>单击Dialog1中的特定元素时,将打开Dialog2.
>当您按Escape关闭Dialog2时,按预期工作并关闭Dialog2.
> 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"); }
}
最佳答案
这一行:
原文链接:https://www.f2er.com/jquery/427951.htmlvar index = $.inArray($(this).attr("id"),$prevIoUsDialog = (index > 0) ? $("#" + openDialogs[index]) : undefined;
将$prevIoUsDialog引用到当前对话框(正在关闭的对话框). index是要关闭的对话框的索引.
因此,稍后在这一行:
if ($prevIoUsDialog) { $prevIoUsDialog.focus(); }
我相信你想要的是这样的:
close: function() {
var index = $.inArray($(this).attr("id"),$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();相反,你避免在对话框周围得到可能令人讨厌的虚线.