Listen to and disable browser carriage return event instances in JavaScript, and listen to javascript

Source: Internet
Author: User

Listen to and disable browser carriage return event instances in JavaScript, and listen to javascript

Js listens to browser carriage return events and supports browsers such as ie6 +, Firefox, and Google.
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
// Register a keyboard event
Document. onkeydown = function (e ){
// Capture the carriage return event
Var ev = (typeof event! = 'Undefined ')? Window. event: e;
If (ev. keyCode = 13 ){
Alert ('catch the carriage return event! ');
}
}
</Script>
</Head>
<Body/>
</Html>

So, how to capture the carriage return event of a specified DOM object? Here, the input label is used as an example to describe:

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
// Register a keyboard event
Document. onkeydown = function (e ){
// Capture the carriage return event
Var ev = (typeof event! = 'Undefined ')? Window. event: e;
If (ev. keyCode = 13 & document. activeElement. id = "msg "){
Alert ("retrieved content:" + document. activeElement. value );
}
}
</Script>
</Head>
<Body>
<Input type = "text" id = "msg" value = ""/>
</Body>
</Html>

So how does js disable the browser carriage return event? We know that in the HTML form area, when you press enter, the browser automatically submits the form by default. The following uses this example to describe how js disables the browser carriage return event:

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
// Register a keyboard event
Document. onkeydown = function (e ){
// Capture the carriage return event
Var ev = (typeof event! = 'Undefined ')? Window. event: e;
If (ev. keyCode = 13 & document. activeElement. id = "msg "){
Return false; // disable the carriage return event.
}
}
</Script>
</Head>
<Body>
<Form action = "form. php">
<Input type = "text" id = "msg" name = "msg" value = ""/>
<Input type = "submit" value = "submit"/>
</Form>
</Body>
</Html>

Related Article

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.