In the text box, the ENTER key to trigger the event's JS code [multi-browser compatible]_javascript tips

Source: Internet
Author: User
It is very simple to determine if a carriage return is pressed:
Copy Code code as follows:

function enterpress () {
if (Event.keycode = = 13) {
...
}
}

IE6 's onkeypress will accept the "carriage return event" and onkeydown will not accept
IE8 onkeypress will not accept the "carriage return event", and OnKeyDown will accept
... Don't dwell on it, two of them.
Copy Code code as follows:

<input type= "text" onkeypress= "enterpress ()" onkeydown= "enterpress ()"/>

However, under the FF, there will be contradictions. FF is onkeypress and onkeydown accept the "carriage return event".
Also, to be able to obtain an event under FF, you need to write this:
Copy Code code as follows:

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

So, if you give any one event incoming parameter event, and another argument is not passed, you can have FF perform only once:
Copy Code code as follows:

&<input type= "text" onkeypress= "enterpress (event)" onkeydown= "enterpress ()"/>

In comprehensive, compatible with IE and FF:
Copy Code code as follows:

<script>
function Enterpress (e) {//Incoming event
var e = e | | window.event;
if (E.keycode = = 13) {
document.getElementById ("Txtadd"). focus ();
}
}
</script>
<body>
<input type= "text" id= "txtname" onkeypress= "enterpress (event)" onkeydown= "enterpress ()"/>
<input type= "text" id= "Txtadd"/>
</body>

--by: The Illusion of bubbles

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.