Create event observer for focus?

前端 未结 2 560
难免孤独
难免孤独 2021-02-01 20:29

Is it possible to have focus events bubble in protoype?

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



        
2条回答
  •  独厮守ぢ
    2021-02-01 21:02

    You can use this :

    function focusInHandler(event){
        Event.element(event).fire("focus:in");
    }
    function focusOutHandler(event){
        Event.element(event).fire("focus:out");
    }
    
    if (document.addEventListener){
        document.addEventListener("focus", focusInHandler, true);
        document.addEventListener("blur", focusOutHandler, true);
    } else {
        document.observe("focusin", focusInHandler);
        document.observe("focusout", focusOutHandler);
    }
    
    document.observe('focus:in', function(event) {
        // Your code
    });
    

    My jsFiddle : http://jsfiddle.net/EpokK/k7RPE/3/

提交回复
热议问题