CSSStyleDeclaration: item() 方法
CSSStyleDeclaration.item() 方法用于根据索引从 CSSStyleDeclaration 对象中返回 CSS 属性名称。
只要提供了参数,此方法就不会抛出异常;如果索引超出范围,则返回空字符串;如果未提供参数,则抛出 TypeError。
语法
js
item(index)
参数
index-
要获取的节点的索引。索引是基于零的。
返回值
指定索引处的 CSS 属性名称的字符串。
JavaScript 有一个特殊的、更简单的语法,用于通过索引从 NodeList 中获取项。
js
const propertyName = style[index];
异常
TypeError-
如果未提供参数,则会抛出此异常。
示例
js
const style = document.getElementById("div1").style;
const propertyName = style.item(1); // or style[1] - returns the second style listed
规范
| 规范 |
|---|
| CSS 对象模型 (CSSOM) # dom-cssstyledeclaration-item |
浏览器兼容性
加载中…