StylePropertyMapReadOnly.get() - Web APIs 编辑
Experimental
This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The get()
method of the StylePropertyMapReadOnly
interface returns a CSSStyleValue
object for the first value of the specified property.
Syntax
var declarationBlock = StylePropertyMapReadOnly.get(property)
Parameters
- property
- The name of the property to retrieve the value of.
Return value
A CSSStyleValue
object.
Examples
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="results"></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');
// Retrieve all computed styles with computedStyleMap()
const styleMap = myElement.computedStyleMap();
// get the <dl> we'll be populating
const stylesList = document.querySelector('#results');
// array of properties we're interested in
const ofInterest = ['font-weight', 'border-left-color', 'color', '--colour'];
// iterate over our properties of interest
for ( let i = 0; i < ofInterest.length; i++ ) {
// properties
const cssProperty = document.createElement('dt');
cssProperty.innerText = ofInterest[i];
stylesList.appendChild(cssProperty);
// values
const cssValue = document.createElement('dd');
// use get() to find the value
cssValue.innerText = styleMap.get(ofInterest[i]);
stylesList.appendChild(cssValue);
}
Specifications
Specification | Status | Comment |
---|---|---|
CSS Typed OM Level 1 The definition of 'get()' in that specification. | Working Draft | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See Also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论