这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

get() 方法在 StylePropertyMapReadOnly interface returns a CSSStyleValue object for the first value of the specified property.

句法

var declarationBlock = StylePropertyMapReadOnly.get(property)
					

参数

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.
工作草案 初始定义。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: