点击处理程序甚至被调用之前检查复选框 [英] Checkboxes are being checked before click handler is even called
问题描述
我有以下片段: http://jsfiddle.net/L7rne/5/ 和 http://jsfiddle.net/L7rne/6/
事实证明,如果您在点击事件处理程序中暂停脚本的执行,则会检查该复选框,即使有 event.preventDefault()
。
It turns out that if you pause execution of the script in the click event handler, then the checkbox is checked, even though there is event.preventDefault()
.
我在Firefox 7和Chrome 15中尝试过,两者都以相同的方式进行。
I tried this in Firefox 7 and Chrome 15 and both behave in the same way.
我几乎肯定这是一个正确的行为,但不知何故,我找不到任何参考为什么这样。
I'm almost sure that it's a correct behaviour but somehow I cannot find any reference why it's like that.
有人可以指向正确的方向吗?
Could someone point me in the right direction?
当您从复选框开始时,它变得更加有趣:
http: //jsfiddle.net/L7rne/8/ - 该复选框被取消选中,但其值保持选中
http://jsfiddle.net/L7rne/9/ - 该复选框不被选中,但其值变为 false
When you start with checked checkboxes then it becomes even more interesting: http://jsfiddle.net/L7rne/8/ - the checkbox becomes unchecked but its value stays checked http://jsfiddle.net/L7rne/9/ - the checkbox becomes unchecked but its value becomes false
(这与使用 checkbox.checked
和的纯Java代码一致checkbox.getAttribute('checked')
,所以我没有粘贴另一个jsfiddle片段)
(This is consistent with pure Javascript code using checkbox.checked
and checkbox.getAttribute('checked')
, so I'm not pasting another jsfiddle snippets)
COBOL恐龙评论之一指向我这样: http://www.w3.org/ TR / DOM-Level-2-Events / events.html#事件流取消,但IMHO只能证实我的观点...
COBOLdinosaur in one of the comments pointed me to this: http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation, but IMHO that only confirms my point of view...
推荐答案
当事件处理程序触发时,事件已经发生,浏览器正在重新渲染。如果处理程序被暂停,那么浏览器完成工作,因为没有阻止它。
By time the event handler fires, the event has already taken place, and the browser is re-rendering in response. If the handler gets suspended, then the browser completes the work because nothing prevented it.
它像打电话给警察去银行抢劫,他们正在交通事故。强盗逃走了行为完成了。
Its like calling the police to a bank robbery, and they are in a traffic accident on the way. The robbers get away. The deed is done.
这篇关于点击处理程序甚至被调用之前检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!