我正在编写一个表单验证脚本,并希望在onblur事件触发时验证给定的字段。我还想使用事件冒泡,这样我就不必将onblur事件附加到每个单独的表单字段。不幸的是,onblur事件没有冒泡。只是想知道是否有人知道一个优雅的解决方案,可以产生同样的效果。
发布于 2009-10-06 14:10:04
ppk有一种技术可以解决这个问题,包括IE的必要变通方法:http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
发布于 2009-10-06 14:16:11
对于符合标准的浏览器,您需要使用事件捕获(而不是冒泡);对于IE,您需要使用focusout:
if (myForm.addEventListener) {
// Standards browsers can use event Capturing. NOTE: capturing
// is triggered by virtue of setting the last parameter to true
myForm.addEventListener('blur', validationFunction, true);
}
else {
// IE can use its proprietary focusout event, which
// bubbles in the way you wish blur to:
myForm.onfocusout = validationFunction;
}
// And of course detect the element that blurred in your handler:
function validationFunction(e) {
var target = e ? e.target : window.event.srcElement;
// ...
}有关更有趣的细节,请参阅http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
发布于 2012-10-23 18:47:36
使用'Focusout‘事件,因为它有Bubble up effect..thanks。
https://stackoverflow.com/questions/1525718
复制相似问题