如何检测div何时失去焦点?

不是我自己

给定以下标记,我想检测编辑器何时失去焦点:

<div class="editor">
    <input type="text"/>
    <input type="text"/>
</div>
<div class="editor">
    <input type="text"/>
    <input type="text"/>
</div>
<button>GO</button>

编辑:随着用户通过输入元素进行制表,以及每个编辑器div失去焦点(这意味着他们在div之外进行制表),将加载类添加到失去焦点的div中。

jQuery的这一部分是我期望的工作,但是它什么也没做:

$(".editor")
.blur(function(){
    $(this).addClass("loading");
});

在添加控制台日志并意识到它在输入的每个焦点外触发之前,这似乎是可行的。

$('div.editor input').focus( function() {
    $(this).parent()
        .addClass("focused")
        .focusout(function() {
            console.log('focusout');
            $(this).removeClass("focused")
                   .addClass("loading");        
        });
});

这是我一直在努力的测试用例的jsfiddle我知道我在这里缺少一些基本的东西。有人可以启发我吗?

编辑:在下面的一些评论后,我几乎以我想要的方式工作了。现在的问题是检测焦点何时更改为编辑器div之外的某个位置。这是我当前的实现:

function loadData() {
    console.log('loading data for editor ' + $(this).attr('id'));
    var $editor = $(this).removeClass('loaded')
        .addClass('loading');

    $.post('/echo/json/', {
        delay: 2
    })
        .done(function () {
        $editor.removeClass('loading')
            .addClass('loaded');
    });
}

$('div.editor input').on('focusin', function () {
    console.log('focus changed');
    $editor = $(this).closest('.editor');
    console.log('current editor is ' + $editor.attr('id'));
    if (!$editor.hasClass('focused')) {
        console.log('switched editors');

        $('.editor.focused')
            .removeClass('focused')
            .each(loadData);

        $editor.addClass('focused');
    }
})

稍微复杂一点,并且使用类来表示状态。我还添加了下一个复杂性,即当编辑器失去焦点时进行异步调用。这是我当前工作的基础。

去编码

如果您希望将输入对的输入输出视为合并到单个控件中,则需要查看获得焦点的元素是否在相同的位置editor您可以通过使用setTimeout0将检查延迟一个周期(直到所有当前任务完成)。

$('div.editor input').focusout(function () {
    var $editor = $(this).closest('.editor');
    // wait for the new element to be focused
    setTimeout(function () {
        // See if the new focused element is in the editor
        if ($.contains($editor[0], document.activeElement)) {
            $editor.addClass("focused").removeClass("loading");
        }
        else
        {
            $editor.removeClass("focused").addClass("loading");
        }
    }, 1);
});

JSFiddle:http : //jsfiddle.net/TrueBlueAussie/8s8ayv52/18/

要完成拼图(获取初始绿色状态),您还需要捕捉focusin事件,并查看事件是否来自同一编辑器(将先前关注的元素保存在全局变量中,等等)。

旁注:我最近不得不编写一个jQuery插件来对所有元素组进行所有这些操作。它生成自定义groupfocusgroupblur事件,以使其余代码更易于使用。

更新1:http : //jsfiddle.net/TrueBlueAussie/0y2dvxpf/4/

根据您的新示例,您可以反复捕捉焦点而不会造成损坏,因此根本不必跟踪先前的焦点。使用我之前的setTimeout示例可以解决在div外部单击时遇到的问题。

$('div.editor input').focusin(function(){
    var $editor = $(this).closest('.editor');
    $editor.addClass("focused").removeClass("loading");

}).focusout(function () {
    var $editor = $(this).closest('.editor');
    // wait for the new element to be focused
    setTimeout(function () {
        // See if the new focused element is in the editor
        if (!$.contains($editor[0], document.activeElement)) {
            $editor.removeClass("focused").each(loadData);
        }
    }, 0);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何知道何时在本机textInput中失去焦点?

Mac上的Java:如何检测应用程序何时获得焦点?

我怎么知道EditText何时失去焦点?

如何在iOS的SwiftUI中检测TextField何时失去焦点?

如何检测何时加载内容?

小部件失去焦点时如何截取

单击DIV中的元素时,DIV失去了焦点

使用纯JS或jQuery通过Alt-Tab检测浏览器页面窗口何时失去焦点

UWP:检测应用获得/失去焦点

在Angular2中如何知道ANY表单输入字段何时失去焦点

榆木-如何检测当前焦点

如何检测用户窗体控件是否失去焦点/是否通过鼠标单击与Tab键退出

如何检测何时敲击RPSystemBroadcastPickerView

如何发送按键输入而不失去焦点?

失去焦点时如何隐藏QML窗口

如何在Windows 8.1的拆分视图中检测我的应用何时获得焦点?

如何确定应用程序的控制台窗口何时获得焦点或失去焦点?

是否有可能检测到jSpinner的按钮上失去的焦点

如何检测何时位于可滚动div的底部?

如何显示JPopupMenu而不会失去焦点?

检测片段何时再次位于顶部(具有焦点)

React:如何让React组件按钮失去焦点?

如何检测何时savehtml失败?

如何检测何时删除文本

nodejs程序检测终端上的按键事件失去焦点

如何检测何时在 TextEdit 组件中获得焦点

如何检测 TVML 锁定失去焦点?

通过 Enter Key 检测失去焦点 (Python-Kivy)

如何检测 Material 单选按钮或组何时失去焦点?