简体   繁体   English

当整个页面在Google Chrome中模糊时,模糊会被触发两次

[英]Blur is fired twice when the entire page is blured in Google Chrome

I have dynamic textarea s in the web page that are added by jQuery and I have to detect the blur event for them. 我在网页中有动态textarea ,由jQuery添加,我必须为它们检测blur事件。

JSFIDDLE ; JSFIDDLE ;

The following code work fine until the entire page is blured (move to another tab, alt-tab press etc) when the blur event is called two times. 当模糊事件被调用两次时,以下代码可以正常工作,直到整个页面模糊(移动到另一个选项卡,alt-tab按等)。

// I've already tried with another selector, parent of the textareas
// instead of document, but it's the same result.
$(document).on("blur", "textarea", function () {
    $("body").append("<br />Blured the " + (($(this).index() / 2) + 1) + " textarea.");
});

I am looking for a solution using only on function this way and not like bellow (in this case the issue exists too): 我正在寻找一种只使用on函数的解决方案而不是像bellow(在这种情况下也存在问题):

var element = $("<textarea></textarea><br />");

element.on("blur", function () {
    $("body").append("<br />Blured the " + (($(this).index() / 2) + 1) + " textarea.");
});

...

JSFIDDLE ; JSFIDDLE ;

So the issue is that when the textarea is focused and the page is blured the blur event is fired twice. 所以问题在于,当textarea被聚焦并且页面被模糊时,模糊事件被触发两次。

Which is the solution for this? 这是什么解决方案?

Is this a browser issue? 这是一个浏览器问题吗? How to prevent it? 怎么预防呢?

Update: It seems that it is present only in Google Chrome . 更新:它似乎仅存在于Google Chrome中

OS: Ubuntu 13.04 操作系统:Ubuntu 13.04


I've reported the issue here: http://code.google.com/p/chromium/issues/detail?id=253253 我在此处报告了此问题: http//code.google.com/p/chromium/issues/detail?id = 253253

It's a bit of a hack, but you can use a timeout to make sure the event isn't fired twice : 这有点像黑客,但您可以使用超时来确保事件不会被触发两次:

$("button").on("click", function () {
    $("#test").append("<textarea></textarea><br />");
});

$(document).on("blur", "textarea", function () {
    var self = $(this);
    if (! self.data('fired')) {
        $("body").append("<br />Blured the " + (($(this).index() / 2) + 1) + " textarea.");
    }
    self.data('fired', true);
    setTimeout(function() {
        self.data('fired', false);
    },0);
});

FIDDLE 小提琴

An other possible workaround: 另一种可能的解决方法:

http://jsfiddle.net/tpRgf/4/ http://jsfiddle.net/tpRgf/4/

(function () {
    var lastActive;
    $(document).on("blur", "textarea", function () {
        if (lastActive === this) { return; }
            lastActive = this;
            $("body").append("<br />Blured the " + (($(this).index() / 2) + 1) + " textarea.");

    }).on('focus', "textarea", function () {
        lastActive = null;
    });
})();

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

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