CSSRuleList: item() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

CSSRuleList 接口的 item() 方法返回指定 index 处的 CSSRule 对象,如果指定的 index 不存在则返回 null

语法

js
item(index)

参数

index

一个整数。

返回值

一个 CSSRule

示例

在下面的示例中,我们假设 myRules 列表仅有三个项目。

js
let myRules = document.styleSheets[0].cssRules;
console.log(myRules.item(0)); // Logs the first CSSRule item from this list

// Accessing non-existing items using this method will return null instead of undefined
console.log(myRules.item(5)); // null
console.log(myRules[5]); // undefined

规范

规范
CSS 对象模型 (CSSOM)
# dom-cssrulelist-item

浏览器兼容性