简体   繁体   English

jQuery模糊事件在Chrome中被触发两次

[英]jQuery blur event fired twice in Chrome

I created a simple 'infinite' form with input fields. 我用输入字段创建了一个简单的“无限”形式。 Every time an empty input is focused it creates a new one, and on blur of an empty input field, the field is removed. 每次聚焦空输入时都会创建一个新输入,并且在空输入字段模糊时,该字段将被删除。

See example here 这里的例子

I use the following code to make it all happen: 我使用以下代码来实现这一切:

var $input = $('<div/>').html( $('<input/>').addClass('value') );
$('form').append( $input.clone() );

$('form').on( 'focus', 'input.value', function(e) {

    // Add new input if the focused one is empty
    if(!$.trim(this.value).length) {
        $('form').append( $input.clone() );
    }


}).on( 'blur', 'input.value', function(e) {
    var $this = $(this);

    if( !$.trim(this.value).length ) {
        console.log('REMOVING INPUT');
        $this.parent().remove();
    } else {
        $this.attr('name', 'item-'+$this.val());
    }

});

The problem is however, that in Chrome the blur event is fired twice when I switch to another application ( tab ). 但问题是,在Chrome中,当我切换到另一个应用程序时, blur事件会被触发两次( 标签 )。 This gives an error, because it is not possible to remove the node, since it's already gone: 这会产生错误,因为它不可能删除节点,因为它已经消失了:

Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 

Firefox seems to work fine. Firefox似乎工作正常。

So why is the blur event fired twice and how can I prevent that from happening? 那么为什么blur事件被触发两次,我该如何防止这种情况发生呢?

EDIT - Tried the answer in this question , but no luck. 编辑 - 在这个问题中尝试了答案,但没有运气。 Still get the error message in Chrome, what am I doing wrong? 仍然在Chrome中收到错误消息,我做错了什么?

See updated fiddle 看到更新的小提琴

Is there a way to check if the element still exists? 有没有办法检查元素是否仍然存在? Because the second time blur fires the node is removed. 因为第二次blur触发节点被删除。 $(this).length still is non-zero though. $(this).length仍然是非零。

这看起来像Blink中的一个错误,在Chromium项目的页面上有关于该错误的报告: http//code.google.com/p/chromium/issues/detail?id = 253253

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

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