From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/touch/target/index.html | 77 +++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 files/zh-cn/web/api/touch/target/index.html (limited to 'files/zh-cn/web/api/touch/target') diff --git a/files/zh-cn/web/api/touch/target/index.html b/files/zh-cn/web/api/touch/target/index.html new file mode 100644 index 0000000000..be815271e8 --- /dev/null +++ b/files/zh-cn/web/api/touch/target/index.html @@ -0,0 +1,77 @@ +--- +title: Touch.target +slug: Web/API/Touch/target +tags: + - API + - DOM + - EventTarget + - TouchEvent + - touch + - 属性 + - 移动设备 + - 触摸 +translation_of: Web/API/Touch/target +--- +

{{ APIRef("Touch Events") }}

+ +

概述

+ +

这个属性返回触摸点最初接触的 Element,即使这个触摸点已经移出那个元素的交互区域,甚至移出文档。需要注意的是,如果这个元素在触摸过程中被移除,这个事件仍然会指向它,因此这个事件也不会冒泡到 window 或 document 对象。因此,如果有元素在触摸过程中可能被移除,最佳实践是将触摸事件的监听器绑定到这个元素本身,防止元素被移除后,无法再从它的上一级元素上侦测到从该元素冒泡的事件。

+ +

语法

+ +
var el = touchPoint.target;
+
+ +

返回值

+ +
+
el
+
{{domxref("Touch")}} 对象的目标元素。
+
+ +

示例

+ +

这个例子展示了如何访问 {{domxref("Touch")}} 对象的 {{domxref("Touch.target")}} 属性。{{domxref("Touch.target")}} 属性是最初接触平面的触摸点下的 {{domxref("Element")}} ({{domxref("EventTarget")}}) 。

+ +

在下面的代码片段中,我们假设用户在  source 元素上开始接触,因此初始化了一个或多个触摸点。当这个元素上的 {{event("touchstart")}} 事件处理程序被调用时,每个触摸点的 {{domxref("Touch.target")}} 属性可经事件的 {{domxref("TouchEvent.targetTouches")}} 列表访问。

+ +
// 为'source'元素注册一个触摸监听器
+var src = document.getElementById("source");
+
+src.addEventListener('touchstart', function(e) {
+  // 在这个元素上激活的触点间循环
+  for (var i=0; i < e.targetTouches.length; i++) {
+    console.log("touchpoint[" + i + "].target = " + e.targetTouches[i].target);
+  }
+}, false);
+
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态注释
{{SpecName('Touch Events 2','#dom-touch-target')}}{{Spec2('Touch Events 2')}}Non-stable version.
{{SpecName('Touch Events', '#widl-Touch-target')}}{{Spec2('Touch Events')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.Touch.target")}}

-- cgit v1.2.3-54-g00ecf