触发焦点事件将触发模糊事件(不使用警报和浏览器控制台)
trigger focus event will fires blur event(without use alert and browser console)
<input id = "focus-ipt" type = "text" value = "nothing" />
var $focusIpt = document.querySelector( "#focus-ipt" );
$focusIpt.addEventListener( "blur", function ( e ) {
$focusIpt.value = "blured!";
} );
setTimeout( function () {
$focusIpt.focus();
}, 1000 );
我只是触发了焦点事件,但是在焦点之后触发了模糊事件;
找了半天,关闭控制台,用setTimeout,还是解决不了;真不知道怎么回事,谁能帮帮我?
实际上不是这种情况; focus()
does not automatically trigger blur()
.
blur()
被触发,因为您正在与 DOM 互动。通过单击 fiddle 中的其中一个按钮(例如 Run
或 Tidy
),#focus-ipt
元素不再处于活动状态 - 您正在单击的按钮是。
如果您只是等待超时,什么都不做,blur()
事件将不会发生。一旦您点击关闭输入,blur()
就会发生。
这可以在下面的例子中看到:
var $focusIpt = document.querySelector("#focus-ipt");
$focusIpt.addEventListener("blur", function(e) {
$focusIpt.value = "Blurred!";
});
setTimeout(function() {
$focusIpt.focus();
}, 1000);
<input id="focus-ipt" type="text" id="myText" value="Sample Text">
<button type="button">Click me to lose focus</button>
希望对您有所帮助! :)
<input id = "focus-ipt" type = "text" value = "nothing" />
var $focusIpt = document.querySelector( "#focus-ipt" );
$focusIpt.addEventListener( "blur", function ( e ) {
$focusIpt.value = "blured!";
} );
setTimeout( function () {
$focusIpt.focus();
}, 1000 );
我只是触发了焦点事件,但是在焦点之后触发了模糊事件; 找了半天,关闭控制台,用setTimeout,还是解决不了;真不知道怎么回事,谁能帮帮我?
实际上不是这种情况; focus()
does not automatically trigger blur()
.
blur()
被触发,因为您正在与 DOM 互动。通过单击 fiddle 中的其中一个按钮(例如 Run
或 Tidy
),#focus-ipt
元素不再处于活动状态 - 您正在单击的按钮是。
如果您只是等待超时,什么都不做,blur()
事件将不会发生。一旦您点击关闭输入,blur()
就会发生。
这可以在下面的例子中看到:
var $focusIpt = document.querySelector("#focus-ipt");
$focusIpt.addEventListener("blur", function(e) {
$focusIpt.value = "Blurred!";
});
setTimeout(function() {
$focusIpt.focus();
}, 1000);
<input id="focus-ipt" type="text" id="myText" value="Sample Text">
<button type="button">Click me to lose focus</button>
希望对您有所帮助! :)