It is very easy to determine whether to press Enter:
Copy codeThe Code is as follows:
Function EnterPress (){
If (event. keycode = 13 ){
...
}
}
The onkeypress of IE6 accepts the "carriage return event", while the onkeydown does not.
In IE8, onkeypress does not accept the "carriage return event", while onkeydown does.
... You don't have to worry about this. Write both of them.
Copy codeThe Code is as follows:
<Input type = "text" onkeypress = "EnterPress ()" onkeydown = "EnterPress ()"/>
However, under FF, there will be a conflict. FF is onkeypress and onkeydown both accept the "carriage return event.
At the same time, to be compatible with FF, you need to write as follows:
Copy codeThe Code is as follows:
Function EnterPress (e) {// input the event
Var e = e | window. event;
If (e. keycode = 13 ){
...
}
}
If you pass the parameter event to any event and do not pass the parameter, you can run FF only once:
Copy codeThe Code is as follows:
& <Input type = "text" onkeypress = "EnterPress (event)" onkeydown = "EnterPress ()"/>
In summary, compatible with IE and FF:
Copy codeThe Code is as follows:
<Head>
<Script>
Function EnterPress (e) {// input the event
Var e = e | window. event;
If (e. keycode = 13 ){
Document. getElementById ("txtAdd"). focus ();
}
}
</Script>
</Head>
<Body>
<Input type = "text" id = "txtName" onkeypress = "EnterPress (event)" onkeydown = "EnterPress ()"/>
<Input type = "text" id = "txtAdd"/>
</Body>
-- By: Bubble fantasies