aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/globaleventhandlers/onkeypress/index.html
blob: 266091fc2847441fae83b507509ffc26ad089766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: GlobalEventHandlers.onkeypress
slug: Web/API/GlobalEventHandlers/onkeypress
tags:
  - API
  - HTML DOM
  - Property
  - Reference
translation_of: Web/API/GlobalEventHandlers/onkeypress
---
<div>{{ ApiRef("HTML DOM") }}</div>

<p><strong>onkeypress</strong> 属性用来获取或设置当前元素的keypress事件的事件处理函数.</p>

<h2 id="语法">语法</h2>

<pre class="eval">element.onkeypress = <em>event handling code</em>
</pre>

<h2 id="备注">备注</h2>

<p>当用户在键盘上按下任意键时,<strong>应当</strong>会触发 keypress 事件。然则有些浏览器不会触发某些键的 kerpress 事件。</p>

<h3 id="浏览器不兼容性">浏览器不兼容性</h3>

<p>Webkit-based 浏览器(如 Google Chrome 及 Safari)不会触发方向键的 keypress 事件。</p>

<p>Firefox 不会触发如 SHIFT 键等修改键的 keypress 事件。</p>

<h2 id="示例">示例</h2>

<h3 id="示例1:通过正则表达式在表单域中过滤数字">示例1:通过正则表达式在表单域中过滤数字</h3>

<p>下例演示了 <code>onkeypress </code>事件在一个文本输入框内的用法——将数字输入到表单时过滤输入的内容:</p>

<pre class="brush: html">&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Example&lt;/title&gt;
&lt;script type="text/javascript"&gt;
  function numbersOnly(oToCheckField, oKeyEvent) {
    return oKeyEvent.charCode === 0 || /\d/.test(String.fromCharCode(oKeyEvent.charCode));
  }
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form name="myForm"&gt;
&lt;p&gt;这个文本框只能输入数字(译者注:不用中文输入法的前提下):
&lt;input type="text" name="myInput" onkeypress="return numbersOnly(this, event);" onpaste="return false;" /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>

<h3 id="示例2:捕获隐藏单词的输入">示例2:捕获隐藏单词的输入</h3>

<p>以下示例将在用户在页面的任何位置键入单词“exit”后执行某些操作。</p>

<div class="note"><strong>Note:</strong> A more complete framework for capturing the typing of hidden words is <a class="external external-icon" href="https://github.com/madmurphy/spell.js/">available on GitHub</a>.</div>

<pre class="brush: js">/* Type the word "exit" in any point of your page... */

(function () {

  var sSecret = /* chose your hidden word...: */ "exit", nOffset = 0;

  document.onkeypress = function (oPEvt) {
    var oEvent = oPEvt || window.event, nChr = oEvent.charCode, sNodeType = oEvent.target.nodeName.toUpperCase();
    if (nChr === 0 || oEvent.target.contentEditable.toUpperCase() === "TRUE" || sNodeType === "TEXTAREA" || sNodeType === "INPUT" &amp;&amp; oEvent.target.type.toUpperCase() === "TEXT") { return true; }
    if (nChr !== sSecret.charCodeAt(nOffset)) {
      nOffset = nChr === sSecret.charCodeAt(0) ? 1 : 0;
    } else if (nOffset &lt; sSecret.length - 1) {
      nOffset++;
    } else {
      nOffset = 0;
      /* do something here... */
      alert("Yesss!!!");
      location.assign("http://developer.mozilla.org/");
    }
    return true;
  };
})();</pre>

<h2 id="规范">规范</h2>

<table class="spectable standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG','webappapis.html#handler-onkeypress','onkeypress')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>


<p>{{Compat("api.GlobalEventHandlers.onkeypress")}}</p>
</div>