事件冒泡和 onblur 事件
我正在编写一个表单验证脚本,并希望在 onblur 事件触发时验证给定字段。我还想使用事件冒泡,这样我就不必将 onblur 事件附加到每个单独的表单字段。不幸的是,onblur 事件不会冒泡。只是想知道是否有人知道可以产生相同效果的优雅解决方案。
I'm writing a form validation script and would like to validate a given field when its onblur event fires. I would also like to use event bubbling so i don't have to attach an onblur event to each individual form field. Unfortunately, the onblur event doesn't bubble. Just wondering if anyone knows of an elegant solution that can produce the same effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将需要对符合标准的浏览器使用事件捕获(而不是冒泡),对 IE 使用
focusout
:请参阅 http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html 了解有趣的细节
You're going to need to use event capturing (as opposed to bubbling) for standards-compliant browsers and
focusout
for IE:See http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html for the juicy details
ppk 为此提供了一种技术,包括 IE 所需的解决方法: http: //www.quirksmode.org/blog/archives/2008/04/delegating_the.html
ppk has a technique for this, including the necessary workarounds for IE: http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
aa,您可以简单地在表单上添加 onblur 事件,并且每次您将焦点更改到其中的任何元素时都会调用验证
aa, you can simply add the onblur event on the form, and will call the validation every time you change focus on any of the elements inside it