创建焦点的事件观察者? [英] Create event observer for focus?

查看:82
本文介绍了创建焦点的事件观察者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能让焦点事件冒充原型?

Is it possible to have focus events bubble in protoype?

我试图阻止必须在每个输入元素上分配一个观察者。

I am trying to prevent having to assign an observer on every input element.

<script language="javascript" type="text/javascript">
document.observe('dom:loaded', function() {

    // Doesn't work
    $('editForm').observe('focus', function(evnt){
        console.log('FOCUS!');
    });

    // Works
    $('editForm').select('INPUT').each(function(elem) {
        elem.observe('focus', function(evnt){
            console.log('FOCUS!');
        });
    });

}); 
</script>

<form method="post" name="editForm" id="editForm" action="">
<input type="text" name="foobar" />
</form>


推荐答案

焦点和模糊事件不会冒泡。

focus and blur events don't bubble.

您可以在捕获阶段触发事件处理程序。当使用标准DOM方法时,你会写

You can fire event-handler during capturing phase. When using standard DOM methods, you would write

document.addEventListener('focus',function(e){/*some code */}, true);

'true'值最重要。

the 'true' value is here most important.

问题在于IE不支持捕获事件传播的阶段,但对于IE,您可以使用focusin和focusout事件,这与焦点和模糊事件不同,会产生泡沫。我建议您阅读Peter Paul Koch撰写的有关此主题的文章 。其他浏览器(Firefox,Opera,Safari)可能(我没有测试它)支持DOMFocusIn,DOMFocusOut等事件,它们是IE焦点和焦点事件的等价物。

The problem is that IE doesn't support capturing phase of event propagation, but for IE you can use focusin and focusout events, which - unlike focus and blur events - do bubble. I recommend reading an article on this topic written by Peter Paul Koch. Other browsers (Firefox, Opera, Safari) probably (I didn't test it) support events like DOMFocusIn, DOMFocusOut which are equivalents for IE's focusin and focusout events.

这篇关于创建焦点的事件观察者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆