事件冒泡和onblur事件

时间:2009-10-06 13:50:12

标签: javascript event-bubbling onblur

我正在编写表单验证脚本,并希望在其onblur事件触发时验证给定字段。我还想使用事件冒泡,所以我不必将onblur事件附加到每个单独的表单字段。不幸的是,onblur事件并没有泡沫。只是想知道是否有人知道可以产生相同效果的优雅解决方案。

5 个答案:

答案 0 :(得分:9)

对于符合标准的浏览器和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

答案 1 :(得分:4)

  

使用' Focusout '事件,因为它有起泡效果..谢谢。

答案 2 :(得分:2)

ppk有一种技巧,包括IE的必要解决方法:http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html

答案 3 :(得分:0)

要在SELECT TO_CHAR(AUF_KOPF.ERFASS_DAT, 'IW') AS "Week No", COUNT( CASE WHEN a.KOPF_SACH IN ('EDI', 'MODEM') THEN a.AUF_NR END ) AS "Total Orders", COUNT( CASE WHEN a.KOPF_SACH NOT IN ('EDI', 'MODEM') THEN a.AUF_NR END ) AS "Other Orders" FROM AUF_KOPF a INNER JOIN ORDER_TOTALS o ON ( a.AUF_NR = o.ORDER_NO ) WHERE a.ERFASS_DAT = '<|Current year|>' GROUP BY To_Char( a.ERFASS_DAT, 'IW' ) ORDER BY "Week No" 事件中使用冒泡,然后可以使用addEventListener()并将useCapture参数设置为 false 。像这样:

onblur

Firefox 51及更低版本不支持yourFormInput.addEventListener('blur', yourFunction, false); 事件。因此,请勿使用onfocusout代替onfocusout事件。

答案 4 :(得分:-2)

aa,您只需在表单上添加onblur事件,并在每次更改对其中任何元素的焦点时调用验证