大网数据广告活动
Web 标准与前端开发

StylePropertyMapReadOnly

· 52CE 我爱测

The **`StylePropertyMapReadOnly`** interface of the CSS Typed Object Model API provides a read-only representation of a CSS declaration block that is an alternative to CSSStyleDeclaration. Retrieve an instance of this interface using Element.computedStyleMap.

CSS Typed Object Model API

The StylePropertyMapReadOnly interface of the CSS Typed Object Model API provides a read-only representation of a CSS declaration block that is an alternative to CSSStyleDeclaration. Retrieve an instance of this interface using Element.computedStyleMap.

Instance properties

  • StylePropertyMapReadOnly.size
    • : Returns an unsigned integer containing the size of the StylePropertyMapReadOnly object.

Instance methods

  • StylePropertyMapReadOnly.entries()
    • : Returns an array of a given object's own enumerable property [key, value] pairs, in the same order as that provided by a Statements/for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
  • StylePropertyMapReadOnly.forEach()
    • : Executes a provided function once for each element of StylePropertyMapReadOnly.
  • StylePropertyMapReadOnly.get()
    • : Returns the value of the specified property.
  • StylePropertyMapReadOnly.getAll()
    • : Returns an array of CSSStyleValue objects containing the values for the provided property.
  • StylePropertyMapReadOnly.has()
    • : Indicates whether the specified property is in the StylePropertyMapReadOnly object.
  • StylePropertyMapReadOnly.keys()
    • : Returns a new array iterator containing the keys for each item in StylePropertyMapReadOnly.
  • StylePropertyMapReadOnly.values()
    • : Returns a new array iterator containing the values for each index in the StylePropertyMapReadOnly object.

Examples

Basic usage

We have to have an element to observe:

<p>
  This is a paragraph with some text. We can add some CSS, or not. The style map
  will include all the default and inherited CSS property values.
</p>
<dl id="output"></dl>

We add a touch of CSS with a custom property to better demonstrate the output:

p {
  --some-variable: 1.6em;
  --some-other-variable: translateX(33vw);
  --another-variable: 42;
  line-height: var(--some-variable);
}

We add JavaScript to grab our paragraph and return a definition list of all the default CSS property values using Element.computedStyleMap().

// get the element
const myElement = document.querySelector("p");

// get the <dl> we'll be populating
const stylesList = document.querySelector("#output");

// Retrieve all computed styles with computedStyleMap()
const stylePropertyMap = myElement.computedStyleMap();

// iterate through the map of all the properties and values, adding a <dt> and <dd> for each
for (const [prop, val] of stylePropertyMap) {
  // properties
  const cssProperty = document.createElement("dt");
  cssProperty.innerText = prop;
  stylesList.appendChild(cssProperty);

  // values
  const cssValue = document.createElement("dd");
  cssValue.innerText = val;
  stylesList.appendChild(cssValue);
}

Examples

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。