javascript 在 onchange 事件之前触发 onblur 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18215263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fire onblur event before onchange event
提问by Joel
I have a large form that contains several text input fields. Essentially, I need to handle the onchange
event for all fieldsand the onblur
events for somefields. When a change is made to a field and the field loses focus, both events fire (which is the correct behavior). The only issue is that I would like to handle the onblur
event before I handle the onchange
event.
我有一个包含多个文本输入字段的大表单。从本质上讲,我需要处理onchange
事件的所有字段和onblur
事件的一些领域。当对字段进行更改并且该字段失去焦点时,两个事件都会触发(这是正确的行为)。唯一的问题是我想在处理onblur
事件之前处理onchange
事件。
After some testing in ie and Firefox, it seems that the default behavior is to fire the onchange
event before onblur
. I have been using the following code as a test...
在 ie 和 Firefox 中进行了一些测试后,似乎默认行为是onchange
在onblur
. 我一直在使用以下代码作为测试...
<html>
<body >
<input type="text" value="here is a text field" onchange="console.log('Change Event!')" onblur="console.log('Blur Event!')" >
</body>
</html>
Which brings me to my questions:
这让我想到了我的问题:
It seems that this behavior is consistent across browsers. Why does
onchange
fire first?Since I cannot handle the
onblur
event for every input element, is there a way I can getonblur
to fire before handling theonchange
event?
似乎这种行为跨浏览器是一致的。为什么
onchange
先火?由于我无法处理
onblur
每个输入元素的事件,有没有办法onblur
在处理onchange
事件之前触发?
回答by alexsh
The reason
onchange
fires first is that once the element loses focus (i.e. 'blurs') the change is usually complete (I say usually because a script can still change the element without user interaction).For those elements that need
onblur
handled first, you can disable theonchange
handler and fire theonchange
(or even a custom event) from theonblur
handler. This will ensure the correct order even though it is more work. To detect change, you can use a state variable for that field.
首先
onchange
触发的原因是,一旦元素失去焦点(即“模糊”),更改通常就完成了(我说通常是因为脚本仍然可以在没有用户交互的情况下更改元素)。对于那些需要
onblur
首先处理的元素,您可以禁用onchange
处理程序并从处理程序中触发onchange
(甚至自定义事件)onblur
。这将确保正确的顺序,即使它是更多的工作。要检测更改,您可以为该字段使用状态变量。
As a general remark though, the need for such synchronicity is a sign that the approach you are using to solve whatever problem you are solving might need more work even though sometimes it cannot be avoided. If you are sure this is the only way, try one of these methods!
不过,一般来说,对这种同步性的需求表明,您用来解决正在解决的任何问题的方法可能需要更多的工作,即使有时无法避免。如果您确定这是唯一的方法,请尝试其中一种方法!
EDIT: Just to elaborate on the last point, you would have to follow some assumptions about your event model. Are you assuming that each change
event is followed by a blur
and goes unprocessed otherwise, or would you like to process each change
but those that are followed by a blur
get further processing after whatever onblur
does with them? In any case if you want to enforce the order the handlers would need access to a common resource (global variable, property, etc.). Are there other event types you might want to use? (input
?). Finally, this link has some details for the change
event for Mozilla browsers:
https://developer.mozilla.org/en-US/docs/Web/Reference/Events/change.
The third 'bullet' addresses the issue of event order.
编辑:为了详细说明最后一点,您必须遵循有关事件模型的一些假设。您是否假设每个change
事件后面都跟有 ablur
并且不进行处理,或者您想处理每个事件,change
但在对它们进行blur
任何处理之后进行进一步处理onblur
?在任何情况下,如果您想强制执行顺序,处理程序将需要访问公共资源(全局变量、属性等)。您可能还想使用其他事件类型吗?(input
?)。最后,此链接包含change
Mozilla 浏览器事件的一些详细信息:https:
//developer.mozilla.org/en-US/docs/Web/Reference/Events/change。第三个“子弹”解决了事件顺序的问题。
回答by Andrew Odri
This is a bit of hack, but it seems to do the trick on most browsers:
这是一个小技巧,但它似乎在大多数浏览器上都能奏效:
<input type="text" value="Text Input" onchange="setTimeout(function(){console.log('Change Event!')}, 0);" onblur="console.log('Blur Event!');" />
You can see a fiddle of it in action here: http://jsfiddle.net/XpPhE/
你可以在这里看到它的操作:http: //jsfiddle.net/XpPhE/
Here is a little background information on the setTimeout(function, 0)
trick: http://javascript.info/tutorial/events-and-timing-depth
这里有一些关于这个setTimeout(function, 0)
技巧的背景信息:http: //javascript.info/tutorial/events-and-timing-depth
Hope that helps :)
希望有帮助:)