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>