JS Code for the carriage return key trigger event in the text box [multi-browser compatibility]

Source: Internet
Author: User

It is very easy to determine whether to press Enter: CopyCode The 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 codeCode: function enterpress (e) {// input 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: <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.