Focusout event loop

2019-07-22 06:24发布

问题:

I've got a problem. I'm using jQuery 1.8.2. I want to react to a focusout event and reset the foucs to the element that was left under special conditions. Now i got this really simple codesnippet which gets caught in a loop in safari and I don't get it why this happens... Maybe you can help me, here's a really simple example:

JS:

$("#test").focusout( function () {
    alert("FocusOut event got triggered.");
    $("#test").focus();
});​

HTML:

<input type="text" id="test" />​

Maybe you want to try this demo: http://jsfiddle.net/ds38v/3/ Just click into the input-box and leave it again.

回答1:

There are some browser differences concerning this issue. Therefore use a timeout to "simulate" it like a "leaveFocus"- and afterwards again a "focus"-Event:

$("#test").focusout( function () {
    window.setTimeout(function() {
        $("#test").focus();
    },1);
});​

Like this it's working.

I hope it helps...