Excluding form fields from keypress handler assigned to body

前端 未结 1 1905
-上瘾入骨i
-上瘾入骨i 2021-01-19 19:31

I have a keypress handler on a web page assigned to the body element. I really do want it to be active anywhere in the web page. Or so I thought. The k

相关标签:
1条回答
  • 2021-01-19 20:06

    It would be easier simply to check which element triggered the event in your keypress handler and filter out input elements:

    document.onkeypress = function(evt) {
        evt = evt || window.event;
        var target = evt.target || evt.srcElement;
        if ( !/INPUT|TEXTAREA|SELECT|BUTTON/.test(target.nodeName) ) {
            // Do stuff
        }
    };
    
    0 讨论(0)
提交回复
热议问题