我正在使用带有WPBakery网站生成器的WordPress。
我正在尝试捕获屏幕中特定元素的CSS属性(“font-size”)。为了捕获元素及其属性,我使用了JavaScript。
我正在运行以下脚本:
var v = document.getElementsByClassName("cb-img-area");
v[0].style.fontSize;
输出是"",即使这是类的CSS -
.cb-img-area {
font-size: 72px;
margin-bottom: 25px;
margin-right: 0;
float: left;
width: 100%;
-webkit-transition: all 250ms ease-in-out;
-moz-transition: all 250ms ease-in-out;
-o-transition: all 250ms ease-in-out;
transition: all 250ms ease-in-out;
}
如何获取类的font-size属性?
谢谢
发布于 2020-07-28 07:49:54
原始here
仅仅获取元素的style.fontSize可能不起作用。如果font-size是由样式表定义的,这将报告"“(空字符串)。
您应该使用window.getComputedStyle。
var el = document.getElementById('foo');
var style = window.getComputedStyle(el, null).getPropertyValue('font-size');
var fontSize = parseFloat(style);
// now you have a proper float for the font size (yes, it can be a float, not just an integer)
el.style.fontSize = (fontSize + 1) + 'px';https://stackoverflow.com/questions/63124570
复制相似问题