我们可以通过该getComputedStyle
方法获取所有的 CSS 样式:
const styles = window.getComputedStyle(ele, null);
从那里,很容易获得特定风格的价值:
const bgColor = styles.backgroundColor;
对于具有以连字符 (-) 开头的供应商前缀的样式,我们可以通过传递样式来获取样式值:
const textSizeAdjust = styles['-webkit-text-size-adjust'];
该getPropertyValue
方法产生相同的结果:
const bgColor = styles.getPropertyValue('background-color');
// Or turn the parameter to camelCase format:
const bgColor = styles.getPropertyValue('backgroundColor');