CSSStyleDeclaration.item()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

CSSStyleDeclaration.item() 通过下标从 CSSStyleDeclaration 返回一个 CSS 属性值。只要传入参数这个方法就不会抛出异常;当传入的下标越界时会返回空字符串,当未传入参数时会抛出一个 TypeError

语法

var propertyName = style.item(index);

参数

  • index 是需要查找节点的索引,索引下标从零开始。

Return value

  • propertyName 是一个 DOMString ,是在特定索引位置下的属性的名称。

使用以下的 javascript 语法可以通过索引从 style 的节点列表中获取对应值:

var propertyName = style[index];

范例

js
var style = document.getElementById("div1").style;
var propertyName = style.item(1); // or simply style[1] - returns the second style listed

规范

Specification
CSS Object Model (CSSOM)
# dom-cssstyledeclaration-item

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
item

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support