Home  >  Article  >  Web Front-end  >  JS定义回车事件(实现代码)_javascript技巧

JS定义回车事件(实现代码)_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:29:401114browse

复制代码 代码如下:

$(function () {
            //定义回车事件
            if (document.addEventListener) {//如果是Firefox
                document.addEventListener("keypress", fireFoxHandler, true);
            }
            else {
                document.attachEvent("onkeypress", ieHandler);
            }

            function fireFoxHandler(evt) {

                if (evt.keyCode == 13) {

                    $("#btnLogin")[0].click();
                }
            }
            function ieHandler(evt) {

                if (evt.keyCode == 13) {
                    $("#btnLogin")[0].click();
                }
            }
        });

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn