还有一个我似乎无法在网上找到答案的基本问题。我可以使用 javascript 轻松更改元素的 CSS 属性,
document.getElementById("ExampleID").style.height="30px";
但是每当我尝试使用
将属性打印到控制台时console.log(document.getElementById("ExampleID").style.height);
它打印一个空行而不是属性。如何打印所需元素的样式属性值?非常感谢
最佳答案
您可以使用 getComputedStyle
let elem = document.getElementById('test');
let ht = window.getComputedStyle(elem, null).getPropertyValue("height");
console.log(ht)
.test {
height: 300px;
width: 300px;
border: 1px solid red;
}
<div class="test" id="test">Test</div>
关于javascript - 我可以使用 Javascript 将 CSS 样式属性打印到控制台吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51638974/