简体   繁体   English

当孩子获得焦点时触发模糊事件

[英]Blur event triggered when child gets focus

I need to perform some action when and item loses its focus.当项目失去焦点时,我需要执行一些操作。

Basically I have something like this:基本上我有这样的事情:

<div tabindex="1">
    <h1>title 1</h1>
</div>

<div tabindex="2">
    <h1>title 2</h1>

    <textarea></textarea>
</div>

JS: JS:

$('div').blur((e) => { console.log('blur');});

DEMO演示

Now, when you switch between div s it works like a charm, but when the second div has focus, and you click the child-textarea a blur event is triggered.现在,当您在div之间切换时,它就像一个魅力,但是当第二个div具有焦点时,并且您单击 child-textarea 时会触发blur事件。 This is not what I want, because we're still inside the same div.这不是我想要的,因为我们仍然在同一个 div 中。 How can I fix this such that only a blur event is triggered when the click is outside the div ?如何解决此问题,以便在单击位于 div 之外时仅触发模糊事件?

a element has been focus then the last element focused will trigger event 'blur'.一个元素已经被聚焦,那么最后一个被聚焦的元素将触发事件 'blur'。 You may have to design a hack for it...something like:您可能需要为它设计一个 hack ......类似于:

$('div').blur((e) => {
    setTimeout(function() {
        if(!$('textarea').is(':focus')){
            console.log('blur');
        }
    }, 100);
});

setTimeout is important because blur event being trigged in the first then focus event. setTimeout 很重要,因为在第一个然后是焦点事件中触发了模糊事件。

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

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