触发 DOM 元素事件时的事件顺序
我有一个 Javascript 函数绑定到 INPUT 元素(复选框或单选)的单击事件。该函数检查 DOM 元素的“checked”属性,并根据它是否设置来采取操作。
如果用户单击 GUI 中表示的元素,则检查属性的状态反映 GUI 中的外观(即,如果用户单击激活了该元素,则检查属性为 true)。
但是,如果我触发单击事件(使用 jQuery),那么当调用绑定到单击的函数时,检查选中属性的状态不会显示新状态。然而,GUI 确实得到了更新,以正确表示实际状态。
这些活动的执行时间是否有明确的顺序?在我看来,在设置检查属性之前调用事件侦听器似乎很奇怪。
I have a Javascript function bound to the click event of an INPUT element (either checkbox or radio). That function examines the 'checked' property of the DOM element and takes action depending upon whether it is set or not.
If the user clicks on the element as represented in the GUI, the state of the checked property reflects the appearance in the GUI (that is, if the user's click activated the element, the checked property is true).
However, if I trigger the click event (using jQuery), then, when the bound-to-the-click function is invoked, examining the state of the checked property does NOT show the new state. However, the GUI does get updated to present proper representation of the actual state.
Is there a defined order to when these activities are performed? It seems odd to me that the event listener would be invoked before the checked property is set.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的。通过 jQuery 调用的 .click() 与直接在元素上调用 .click() 的行为不同。请参阅此处:
http://jsfiddle.net/g4aVm/6/
我认为这表明jQuery 在这里做了一些非标准/有缺陷的事情。
(我有点守旧,不相信 jQuery 具有简单的 DOM 内容。使用库来复制已经存在的内容并没有什么好处,但这是一个不同的讨论。)
Interesting. The .click() called through jQuery has a different behaviour than calling .click() direct on the element. See here :
http://jsfiddle.net/g4aVm/6/
I would take this to indicate that jQuery is doing something non-standard/buggy here.
(I'm slight old fashioned and don't trust jQuery with simple DOM stuff. There's not much to gain by using a library to replicate what already exists, but that's a different discussion.)