这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
get()
方法在
StylePropertyMapReadOnly
interface returns a
CSSStyleValue
object for the first value of the specified property.
var declarationBlock = StylePropertyMapReadOnly.get(property)
The name of the property to retrieve the value of.
A
CSSStyleValue
对象。
Let's get just a few properties and values. Let's start by creating a link inside a paragraph in our HTML, and adding a definition list which we will populate with JavaScript:
<p> <a href="https://example.com">Link</a> </p> <dl id="regurgitation"></dl>
We add a bit of CSS, including a custom property and an inhertable property:
p {
font-weight: bold;
}
a {
--colour: red;
color: var(--colour);
}
We use the Element's
computedStyleMap()
to return a
StylePropertyMapReadOnly
object. We create an array of properties of interest and use the StylePropertyMapReadOnly's
get()
method to get only those values.
// get the element
const myElement = document.querySelector('a');
// get the <dl> we'll be populating
const stylesList = document.querySelector('#regurgitation');
// Retrieve all computed styles with computedStyleMap()
const styleMap = myElement.computedStyleMap();
// array of properties we're interested in
const ofInterest = ['font-weight', 'border-left-color', 'color', '--colour'];
// iterate thru our properties of interest
for ( let i = 0; i < ofInterest.length; i++ ) {
// properties
const cssProperty = document.createElement('dt');
cssProperty.appendChild(document.createTextNode(ofInterest[i]));
stylesList.appendChild(cssProperty);
// values
const cssValue = document.createElement('dd');
cssValue.appendChild(document.createTextNode( styleMap.get(ofInterest[i])));
stylesList.appendChild(cssValue);
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Typed OM Level 1
The definition of 'get()' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
实验。期望将来行为有所改变。