StylePropertyMapReadOnly - Web APIs 编辑

Experimental

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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().

Properties

StylePropertyMapReadOnly.size
Returns an unsinged long integer containing the size of the StylePropertyMapReadOnly object.

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 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

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 inherted CSS property values.
</p>
<dl id="output"></dl>

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

p {
   --someVariable: 1.6em;
   --someOtherVariable: translateX(33vw);
   --anotherVariable: 42;
   line-height: var(--someVariable);
}

We add JavaScript to grab our paragraph and return back 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 thru 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);
}

Specifications

SpecificationStatusComment
CSS Typed OM Level 1
The definition of 'StylePropertyMapReadOnly' in that specification.
Working DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:52 次

字数:5884

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文