aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/globaleventhandlers/oninput/index.html
blob: f7489cc18a746ae86da338d09b4d05b54ebb6819 (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
---
title: GlobalEventHandlers.oninput
slug: Web/API/GlobalEventHandlers/oninput
tags:
  - API
  - Event Handler
translation_of: Web/API/GlobalEventHandlers/oninput
---
<div>{{ ApiRef("HTML DOM") }}</div>

<p>{{domxref("GlobalEventHandlers")}}mixin的<code><strong>oninput</strong></code>属性是{{event("Event_handlers")}},它处理{{HTMLElement("input")}},{{HTMLElement("select")}}和 {{HTMLElement("textarea")}} 元素上的 {{event("input")}} 事件。 它还会在{{domxref("HTMLElement.contentEditable", "contenteditable")}} 或 {{domxref("Document.designMode", "designMode")}}打开的元素上处理这些事件。</p>

<div class="blockIndicator note">
<p>注意:与<code>oninput</code>不同的是, {{domxref("GlobalEventHandlers.onchange", "onchange")}} 事件处理程序不一定会针对元素值的每次更改而调用。</p>
</div>

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

<pre class="syntaxbox"><em>target</em>.oninput = <em>functionRef</em>;</pre>

<h3 id="值"></h3>

<p><code>functionRef</code>是一个函数名或函数表达式。该函数接收{{domxref("InputEvent")}} 对象作为唯一参数。</p>

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

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;input type="text" placeholder="Type something here to see its length."size="50"&gt; &lt;p id="log"&gt;&lt;/p&gt;</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">let input = document.querySelector('input');
let log =document.getElementById('log');
input.oninput = handleInput;
function handleInput(e) {
  log.textContent = `The field's value is${e.target.value.length} character(s) long.`;
}</pre>

<h3 id="结果">结果</h3>

<p>{{EmbedLiveSample("示例")}}</p>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("HTML WHATWG", "#ix-handler-oninput", "oninput")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

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

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

<p>The following links discuss compatibility issues and fixes that may be helpful when working with older browsers:</p>

<ul>
 <li><a href="http://blog.danielfriesen.name/2010/02/16/html5-browser-maze-oninput-support/">A HTML5 Browser maze, oninput support</a></li>
 <li><a href="http://www.useragentman.com/blog/2011/05/12/fixing-oninput-in-ie9-using-html5widgets/">Fixing oninput in IE Using html5Widgets</a> includes polyfill for IE6-8</li>
 <li>Mathias Bynens suggests <a href="http://mathiasbynens.be/notes/oninput">binding to both input and keydown</a></li>
 <li><a href="http://help.dottoro.com/ljhxklln.php">oninput event | dottoro</a> has notes about bugginess in IE9</li>
 <li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=312094">Bug 312094 - Add support for &lt;select oninput&gt;</a></li>
</ul>

<h2 id="参见">参见</h2>

<ul>
 <li>{{event("input")}} </li>
</ul>