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

瀏覽器相容性