使用prevent default来接管空格键 [英] Using prevent default to take over spacebar
本文介绍了使用prevent default来接管空格键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些这样的代码来接管空格键的功能:
I have some code like this to take over the space bar's function:
$(document).keypress(function (e) {
e.preventDefault();
if (e.which == 32) {
// func
}
});
不幸的是,这会破坏所有密钥的默认值。
Unfortunately this destroys all key's defaults.
这:
$(document).keypress(function (e) {
if (e.which == 32) {
e.preventDefault();
// func
}
});
很遗憾无效。
怎么能我使它只防止空格键的默认?
How can I make it preventDefault of only spacebar?
谢谢。
推荐答案
试试这个:
//e= e || window.event); you may need this statement to make sure IE doesn't keep the orginal event in motion
var code;
if (e.keyCode) {
code = e.keyCode;
} else if (e.which) {
code = e.which;
}
if (code == 32) {
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
return false;
}
这篇关于使用prevent default来接管空格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文