这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
StylePropertyMapReadOnly
接口的
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()
.
StylePropertyMapReadOnly.size
StylePropertyMapReadOnly
对象。
StylePropertyMapReadOnly.entries()
[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()
StylePropertyMapReadOnly
.
StylePropertyMapReadOnly.get()
Returns the value of the specified property.
StylePropertyMapReadOnly.getAll()
CSSStyleValue
objects containing the values for the provided property.
StylePropertyMapReadOnly.has()
StylePropertyMapReadOnly
对象。
StylePropertyMapReadOnly.keys()
StylePropertyMapReadOnly
.
StylePropertyMapReadOnly.values()
StylePropertyMapReadOnly
对象。
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
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.appendChild(document.createTextNode(prop));
stylesList.appendChild(cssProperty);
// values
const cssValue = document.createElement('dd');
cssValue.appendChild(document.createTextNode(val));
stylesList.appendChild(cssValue);
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Typed OM Level 1
The definition of 'StylePropertyMapReadOnly' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
StylePropertyMapReadOnly
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
entries
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
forEach
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
get
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
getAll
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
has
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
keys
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
size
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
值
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
@@iterator
|
Chrome 66 | Edge 79 | Firefox No | IE No | Opera 53 | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android 47 | Safari iOS No | Samsung Internet Android 9.0 |
完整支持
不支持
实验。期望将来行为有所改变。