aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/globaleventhandlers/onkeypress
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/globaleventhandlers/onkeypress
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/globaleventhandlers/onkeypress')
-rw-r--r--files/zh-cn/web/api/globaleventhandlers/onkeypress/index.html109
1 files changed, 109 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/globaleventhandlers/onkeypress/index.html b/files/zh-cn/web/api/globaleventhandlers/onkeypress/index.html
new file mode 100644
index 0000000000..266091fc28
--- /dev/null
+++ b/files/zh-cn/web/api/globaleventhandlers/onkeypress/index.html
@@ -0,0 +1,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>