Home >Web Front-end >JS Tutorial >JavaScript operates the Enter event of the keyboard (any keyboard event), compatible with multiple browsers_javascript skills

JavaScript operates the Enter event of the keyboard (any keyboard event), compatible with multiple browsers_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:18:561176browse

Without further ado, here’s the code:

Copy the code The code is as follows:
 
document. onkeydown=function()
{
var EventUtil = {};
EventUtil.getEvent = function(){
if(window.event){return window.event;}
else{ return EventUtil.getEvent.caller.arguments[0];}
}
var button = document.getElementById("qw");
if(EventUtil.getEvent().keyCode==13)
{
button.click();
event.returnValue = false;
}
}


For comparison: uploading one does not support IE, but Support FireFox:
Copy code The code is as follows:

window.onkeydown=function()
{
var EventUtil = {};
EventUtil.getEvent = function(){
if(window.event){return window.event;}
else{return EventUtil.getEvent.caller .arguments[0];}
}
    //Bind your button event with id qw


Copy the code The code is as follows:

var button = document.getElementById("qw");
if(EventUtil.getEvent().keyCode==13 )
{
button.click();
event.returnValue = false;
}
}

Do you see any difference?
The above is for reference only.
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