javascript - 由焦点启动时对话框无法正确关闭

标签 javascript jquery jquery-ui jquery-ui-dialog

我希望在关注输入时打开一个对话框。它曾经完美地工作,但我必须升级 jQuery 和/或 jQueryUI,现在 FF 不会删除模式,IE 甚至不会关闭对话框。请参阅http://jsbin.com/EdupOgE/2/举个例子。如我的示例所示,使用单击确实可以正常工作,但不能使用焦点。为什么会发生这种情况以及如何解决它?谢谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> 
        <title>Testing</title>  
        <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.css" type="text/css" rel="stylesheet" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js" type="text/javascript"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js" type="text/javascript"></script>
        <script type='text/javascript'>
            $(document).ready( function() {

                $("#id_focus").focus(function(){$("#dialog").dialog("open");});
                $("#id_click").click(function(){$("#dialog").dialog("open");});

                $("#dialog").dialog({
                    autoOpen    : false,
                    modal       : true,
                    buttons     : [{text    : 'CANCEL',click    : function() {$(this).dialog("close");}}]    
                });

        });</script>

    </head>

    <body>
        None:  <input type="text" value="" />
        Click: <input id="id_click" readonly="readonly" value="PORTLAND OR 97232" />
        Focus: <input id="id_focus" readonly="readonly" value="PORTLAND OR 97232" />
        <div id="dialog" title="Title"></div>
    </body>
</html>

最佳答案

您只需将 $("#id_focus").blur(); 添加到焦点函数中,如下所示:

<强> Working Example

$(document).ready(function () {

    $("#id_focus").focus(function () {
        $("#id_focus").blur(); // Important bit
        $("#dialog").dialog("open");
    });
    $("#id_click").click(function () {
        $("#dialog").dialog("open");
    });

    $("#dialog").dialog({
        autoOpen: false,
        modal: true,
        buttons: [{
            text: 'CANCEL',
            click: function () {
                $(this).dialog("close");
            }
        }]
    });

});

来自the API Documentation :

Focus

Upon opening a dialog, focus is automatically moved to the first item that matches the following:

  1. The first element within the dialog with the autofocus attribute
  2. The first :tabbable element within the dialog's content
  3. The first :tabbable element within the dialog's buttonpane
  4. The dialog's close button
  5. The dialog itself

While open, the dialog widget ensures that tabbing cycles focus between elements within the dialog itself, not elements outside of it. Modal dialogs additionally prevent mouse users from clicking on elements outside of the dialog.

Upon closing a dialog, focus is automatically returned to the element that had focus when the dialog was opened.

强调最后一点是我的

因此,如果在对话框关闭后焦点返回到 #id_focus,它应该在关闭对话框后立即尝试重新打开对话框...我想这种循环功能不会带来任何好处这很可能是你遇到麻烦的原因......

关于javascript - 由焦点启动时对话框无法正确关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18577012/

相关文章:

javascript - 可拖动元素被放到哪里了?

javascript - Chart JS - 基于时区的 X 轴

javascript - 如何在 Angular4 中根据日期对对象数组进行排序?

javascript - 条件注释适用于除 IE 8 及更低版本之外的所有浏览器

php - CodeIgniter 中的 jQuery,在 View 内还是在外部 js 文件中?

Jquery UI 可在更改事件上排序项目位置

javascript - 根据给定条件对表 tr 进行排序

javascript - 为满足的每个部分包裹 li 项目

jQuery 对话框关闭将浏览器滚动到顶部

css - 在 jQuery UI CSS 框架中使用矢量图标作为 Web 字体而不是 PNG