在 ENTER 键按下时调用 javascript 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18189679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:01:38  来源:igfitidea点击:

call javascript function on ENTER key press?

javascripthtml

提问by Simon Presto

I am trying to scroll down the page using javascript.

我正在尝试使用 javascript 向下滚动页面。

I have this code which works fine if if i use javascript:pageScroll()in a link or button:

如果我javascript:pageScroll()在链接或按钮中使用,我有这个代码可以正常工作:

<SCRIPT LANGUAGE="Javascript">
function pageScroll() {
        window.scrollBy(0,50); // horizontal and vertical scroll increments
        scrolldelay = setTimeout('pageScroll()',100); // scrolls every 100 milliseconds
}
</SCRIPT>

but i need to find out how i can call the pageScroll() function if the user presses enter on their keyboard?

但我需要找出如果用户按下键盘上的 Enter 键,我如何调用 pageScroll() 函数?

any help would be greatly appreciated.

任何帮助将不胜感激。

回答by

$(document).keypress(function(e) {
    if(e.which == 13) {
        //do stuff
    }
});

回答by J D

Use jQuery, you might be interested in .keypress(). Here: http://api.jquery.com/keypress/

使用 jQuery,你可能对.keypress(). 这里:http: //api.jquery.com/keypress/

Also you could have easily avoided this question by doing a simple Google search.

您也可以通过简单的谷歌搜索轻松避免这个问题。