DOMRectList: item() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流浏览器均已支持。

DOMRectListitem() 方法返回列表中指定索引处的 DOMRect,如果索引超出范围则返回 null

语法

js
item(index)

参数

index

一个从零开始的整数,表示要检索的 DOMRectListDOMRect 的位置。

返回值

DOMRectList 中指定索引处的 DOMRect 对象,如果索引大于或等于列表中的矩形数量,则为 null。

示例

js
const rects = document.getElementById("rects").getClientRects();
console.log(rects.length); // Number of rectangles in the DOMRectList

if (rects.length > 0) {
  console.log(rects.item(0)); // Logs the first DOMRect object
}

规范

规范
Geometry Interfaces Module Level 1
# dom-domrectlist-item

浏览器兼容性