aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/event/target
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/event/target
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/event/target')
-rw-r--r--files/zh-cn/web/api/event/target/index.html87
1 files changed, 87 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/event/target/index.html b/files/zh-cn/web/api/event/target/index.html
new file mode 100644
index 0000000000..c2b8544a36
--- /dev/null
+++ b/files/zh-cn/web/api/event/target/index.html
@@ -0,0 +1,87 @@
+---
+title: Event.target
+slug: Web/API/Event/target
+tags:
+ - event.target
+ - 事件委托 (event delegation)
+translation_of: Web/API/Event/target
+---
+<p>{{ ApiRef("DOM") }}</p>
+
+<p>触发事件的对象 (某个DOM元素) 的引用。当事件处理程序在事件的冒泡或捕获阶段被调用时,它与{{domxref("event.currentTarget")}}不同。</p>
+
+<h2 id="Example" name="Example">语法</h2>
+
+<pre><code><code>let theTarget = event.target</code></code></pre>
+
+<h2 id="Example" name="Example">示例</h2>
+
+<p><code>event.target </code>属性可以用来实现<strong>事件委托</strong> (<strong>event delegation</strong>)。</p>
+
+<pre class="brush: js">// Make a list
+var ul = document.createElement('ul');
+document.body.appendChild(ul);
+
+var li1 = document.createElement('li');
+var li2 = document.createElement('li');
+ul.appendChild(li1);
+ul.appendChild(li2);
+
+function hide(e){
+  // e.target 引用着 &lt;li&gt; 元素
+  // 不像 e.currentTarget 引用着其父级的 &lt;ul&gt; 元素.
+  e.target.style.visibility = 'hidden';
+}
+
+// 添加监听事件到列表,当每个 &lt;li&gt; 被点击的时候都会触发。
+ul.addEventListener('click', hide, false);
+</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th>Specification</th>
+ <th>Status</th>
+ <th>Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName("DOM WHATWG", "#dom-event-target", "Event.target")}}</td>
+ <td>{{Spec2("DOM WHATWG")}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName("DOM4", "#dom-event-target", "Event.target")}}</td>
+ <td>{{Spec2("DOM4")}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName("DOM2 Events", "#Events-Event-target", "Event.target")}}</td>
+ <td>{{Spec2("DOM2 Events")}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.Event.target")}}</p>
+
+<p>在 IE6-8 中,事件模型与标准不同。使用非标准的 <a href="http://msdn.microsoft.com/en-us/library/ie/ms536343%28v=vs.85%29.aspx"><code>element.attachEvent()</code></a> 方法绑定事件监听器。在该模型中,事件对象有一个 <code>srcElement</code> 属性,等价于<code>target</code> 属性。</p>
+
+<pre class="brush: js" style="font-size: 14px;">function hide(e) {
+ // 支持 IE6-8
+ var target = e.target || e.srcElement;
+ target.style.visibility = 'hidden';
+}
+</pre>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li><a href="/en/DOM/event/Comparison_of_Event_Targets" title="en/DOM/event/Comparison_of_Event_Targets">Comparison of Event Targets</a></li>
+ <li>{{domxref("Event.currentTarget")}}</li>
+</ul>