Home >Web Front-end >JS Tutorial >Browser script compatibility. Compatibility of events triggered by the enter key in the text box_javascript skills

Browser script compatibility. Compatibility of events triggered by the enter key in the text box_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:24:421127browse

It is very simple to determine whether the pressed key is Enter:

Copy code The code is as follows:

function EnterPress(){
if(event.keycode == 13){
...
}
}

IE6’s onkeypress will accept the "Enter event" , and onkeydown will not accept
IE8's onkeypress will not accept "Enter event", but onkeydown will accept
... Don't worry about this, just write both
Copy code The code is as follows:



However, when it comes to FF, there will be contradictions. FF accepts "Enter events" onkeypress and onkeydown.
At the same time, in order to be compatible with FF, it can be obtained below event, you need to write it like this:
Copy code The code is as follows:

function EnterPress(e){ //Incoming event
var e = e | window.event;
if(e.keycode == 13){
...
}
}

Then, as long as you pass the parameter event to any event and pass no parameters to the other, you can make FF only execute once:
Copy code The code is as follows:

&

In summary, compatible with IE and FF:
Copy code The code is as follows:


<script> <br>function EnterPress(e){ //Incoming event <br>var e = e | window.event; <br>if(e.keycode == 13){ <br>document.getElementById("txtAdd").focus(); <br>} <br>} <br></script>






--by:Bubble Fantasy
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