CSSStyleDeclaration.length

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.

这是一个只读的属性,它返回一个指定元素声明过的样式个数

语法

var num = styles.length;

指定元素上明确声明过的样式属性个数。

示例

获取下面 HTML 元素明确设置过的属性个数:

html
<div
  id="div1"
  style="margin: 0 10px; background-color: #CA1; font-family: monospace"></div>

JavaScript 代码:

var myDiv = document.getElementById('div1'); var divStyle = myDiv.style; var len = divStyle.length; // 6

规范

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

浏览器兼容性

BCD tables only load in the browser