javascript--onkeypress事件处理

本文详细介绍了 onkeydown 事件的定义、用法及其语法。解释了如何通过 JavaScript 控制键盘输入,并提供了一个实用示例,展示如何阻止用户在文本框内输入数字。

源自:http://www.fzs8.net/Java/JavaScript/2007-05-25/3969.html

 

Definition and Usage
定义与用法

The onkeydown event occurs when a keyboard key is pressed or held down.
当按下或按住键盘的按键时触发onkeydown事件。

Syntax
语法

 

onkeypress="所要执行的脚本"

 

Parameter
参数
Description
注释
SomeJavaScriptCode
所要执行的脚本
Required. Specifies a JavaScript to be executed when the event occurs.
必选项。当事件触发时所要执行的脚本。

Supported by the following HTML tags:
所支持的HTML标签:

<a>, <acronym>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <body>, <button>, <caption>, <cite>, <code>, <dd>, <del>, <dfn>, <div>, <dt>, <em>, <fieldset>, <form>, <h1> to <h6>, <hr>, <i>, <input>, <kbd>, <label>, <legend>, <li>, <map>, <object>, <ol>, <p>, <pre>, <q>, <samp>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>, <th>, <thead>, <tr>, <tt>, <ul>, <var>

Supported by the following JavaScript objects:
所支持的JavaScript对象:

document, image, link, textarea

 


Tips and Notes
注意

Browser differences: Internet Explorer uses event.keyCode to retrieve the character that was pressed and Netscape/Firefox/Opera uses event.which.
不同浏览器: IE用event.keCode方法获取当前被按下的键盘按键值,而NetScape/FireFox/Opera用的则是event.which


Example
实例

In this example the user cannot type numbers into the input field:
在下面的例子中,用户将无法在文本框内输入数字:

 

<html>
<body>
<script type="text/javascript">
function noNumbers(e)
{
var keynum
var keychar
var numcheck

if(window.event) // IE
{
keynum = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
numcheck = /d/
return !numcheck.test(keychar)
}
</script>

<form>
<input type="text" onkeypress="return noNumbers(event)" />
</form>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值