简体   繁体   中英

How to event handle ALL focus() and blur() triggers on a page?

I have a Timer in javascript that's doing stuff in the background every xxx seconds. however the ajax work in the background seems to interrupt the users typing. I fix this by disabling the timers when focus/blur like one would, however, how can i make this site wide?

I want a way to magically detect any focus/blur events happening in any inputbox/textarea element and call some code, for both blur and focus.. is there a way to achieve this that will work without individually settings up the handlers for each of them?

You may try something like this:

$('input, textarea').on({
    focus: function(e) {
        // ...
    },
    blur: function(e) {
        // ...
    }
});

If you want same function for both events then (probably you don't want this):

$('input, textarea').on('focus blur', function(e) {
    // ...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM