欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

html中用户(在键盘上)按键时触发的事件属性onkeypress

程序员文章站 2022-05-17 21:42:19
...

实例

当用户按键时执行一段 JavaScript:

<input type="text" onkeypress="displayResult()">

浏览器支持

IE

Firefox

Chrome

Safari

Opera

所有主流浏览器都支持 onkeypress 属性。

定义和用法

onkeypress 属性在用户(在键盘上)按键时触发。

提示:相对于 onkeypress 事件的事件次序:

onkeydown onkeypress onkeyup

注释:在任何浏览器中,onkeypress 事件不会被所有按键触发(例如 ALT、CTRL、SHIFT、ESC)。

注释:onkeypress 属性不适用以下元素:<base>、<bdo>、<br>、<head>、<html>、<iframe>、<meta>、<param>、<script>、<style> 或 <title>。

HTML 4.01 与 HTML5 之间的差异

无。

语法

<element onkeypress="script">

属性值

描述
script onkeypress 发生时运行的脚本。
<input class="text" type="text" name="PAGE" size="60" maxlength="2" value="<%=bg.getPAGE()%>" onkeypress="if(event.keyCode<48 || event.keyCode>57) event.returnValue=false;">

报错:

Multiple annotations found at this line:
- Invalid character used in text string (57) event.returnValue=false;">).
- Syntax error on token "||", ) expected
- Attribute value (if(event.keyCode<48 || ) not closed.
- Undefined attribute name (event.keyCode).

把大于57放在前面就对了。。。亲试

以上就是html中用户(在键盘上)按键时触发的事件属性onkeypress的详细内容,更多请关注其它相关文章!