spu*_*dly 4 javascript onblur event-bubbling
我正在编写一个表单验证脚本,并希望在其onblur事件触发时验证给定字段.我还想使用事件冒泡,所以我不必将onblur事件附加到每个单独的表单字段.不幸的是,onblur事件并没有泡沫.只是想知道是否有人知道可以产生相同效果的优雅解决方案.
对于符合标准的浏览器和focusoutIE,您将需要使用事件捕获(而不是冒泡):
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;
// ...
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
| 归档时间: |
|
| 查看次数: |
8061 次 |
| 最近记录: |