如何禁用输入按按钮?禁用输入按键输入按钮
我正在制作游戏,你按下一个按钮,然后一个计数器计数你按下并显示文本,问题是当你按下回车键时计数器快速上升...这里是游戏:“ http://gorillapps.net/games/Button-Clicker.html“。
那么如何禁用JavaScript中默认的按键?
如何禁用输入按按钮?禁用输入按键输入按钮
我正在制作游戏,你按下一个按钮,然后一个计数器计数你按下并显示文本,问题是当你按下回车键时计数器快速上升...这里是游戏:“ http://gorillapps.net/games/Button-Clicker.html“。
那么如何禁用JavaScript中默认的按键?
document.getElementById("countButton").onkeydown = function(e){
if (e.which == 13) //13 is the keycode referring to enter.
{
e.preventDefault(); //this will prevent the intended purpose of the event.
return false; //return false on the event.
}
}
这可以防止通过输入执行按钮。
高级解决方案。回车只允许一次。用户必须放开输入按钮才能重置。
var enterPressed = 0;
document.getElementById("countButton").onkeydown = function(e){
if (e.which == 13)
{
if (!enterPressed)
{
enterPressed = 1;
return true;
}
else
{
e.preventDefault();
return false;
}
}
}
document.getElementById("countButton").onkeyup = function(e){
if (e.keyCode == 13)
{
enterPressed = 0;
}
}
通常我会主张用addEventListener
,然而,这是这样一个简单的网站,目的只有一个,内联的事件在这里是没有问题的。
请提供一些代码。 – Henrik 2015-02-09 14:06:47