TouchList: item() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

item() 方法返回 TouchList 中指定索引处的 Touch 对象。

语法

js
item(index)

参数

index

要检索的 Touch 对象的索引。索引是一个数字,范围从 0 到 TouchList 长度减去 1。

返回值

touchPoint

来自 TouchList 的请求的 Touch 对象。如果索引不小于列表的长度,则返回 null

示例

此代码示例演示了 TouchList 接口的 item 方法和 length 属性的使用。

js
const target = document.getElementById("target");

target.addEventListener("touchstart", (ev) => {
  // If this touchstart event started on element target,
  // set touch to the first item in the targetTouches list;
  // otherwise set touch to the first item in the touches list
  const touch =
    ev.targetTouches.length >= 1
      ? ev.targetTouches.item(0)
      : ev.touches.item(0);
});

规范

规范
触摸事件
# dom-touchlist-item

浏览器兼容性