且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

暂停功能直到输入键被按下javascript

更新时间:2023-12-03 20:26:46

我会创建一个全局变量来查看javascript是否在等待按键。

I would create a global variable to see if the javascript is waiting for a key press.

在脚本的顶部你可以添加

At the top of your script you can add

var waitingForEnter = false;

然后在你的函数中将其设置为true

Then set it to true in your function

function appear()
{
     document.getElementById("firstt").style.visibility="visible";
     waitingForEnter = true;
}

然后...为回车键添加一个监听器

Then...add a listener for the enter key

function keydownHandler(e) {

    if (e.keyCode == 13 && waitingForEnter) {  // 13 is the enter key
        document.getElementById("startrouter").style.visibility="visible";
        waitingForEnter = false; // reset variable
    }
}

// register your handler method for the keydown event
if (document.addEventListener) {
    document.addEventListener('keydown', keydownHandler, false);
}
else if (document.attachEvent) {
    document.attachEvent('onkeydown', keydownHandler);
}

我希望这会有所帮助。这正是我要做的,它可能不是***的方法。

I hope this helps. This is just what I would do, it might not be the best approach.