element onkeydown keycode javascript

前端 未结 4 778
傲寒
傲寒 2021-01-14 09:36

I am using this code snippet to add KeyDown event handler to any element in the html form

for(var i=0;i

        
4条回答
  •  灰色年华
    2021-01-14 10:16

    For detecting Enter, you could use the following code, which will work in all mainstream browsers. It uses the keypress event rather than keydown because Enter produces a printable character:

    ele[i].onkeypress = function(evt) {
        evt = evt || window.event;
        var charCode = evt.keyCode || evt.which;
        if (charCode == 13) {
            alert("Enter");
            // Do stuff here
        }
    };
    

提交回复
热议问题