事件会在键盘按键被按下并释放一个键时发生。浏览器差异:IE 使用 event.keyCode 取回被按下的字符,而 Netscape/Firefox/Opera 使用 event.which。
支持该事件的 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>
支持该事件的 JavaScript 对象:
document, image, link, textarea
例子:
<html> <body> <script type="text/javascript"> function forbitNumber(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 forbitNumber(event)" /> </form> </html>
时间: 2024-12-20 23:02:00