admin管理员组文章数量:1391977
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 keypress
events in textual input forms also activate the body handler, which makes sense, but which I don't want.
Ideally, I'd like to keep the keypress
handler assigned to the body element and somehow exclude just the input forms. Is there any way I can stop the event at the input level and prevent it from propagating to body? (Or is that even the right way to look at HTML DOM events?)
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 keypress
events in textual input forms also activate the body handler, which makes sense, but which I don't want.
Ideally, I'd like to keep the keypress
handler assigned to the body element and somehow exclude just the input forms. Is there any way I can stop the event at the input level and prevent it from propagating to body? (Or is that even the right way to look at HTML DOM events?)
1 Answer
Reset to default 12It 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
}
};
本文标签: javascriptExcluding form fields from keypress handler assigned to bodyStack Overflow
版权声明:本文标题:javascript - Excluding form fields from keypress handler assigned to body - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744677065a2619176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论