我可以使用Javascript将CSS样式属性打印到控制台吗? [英] Can I print a CSS style property to the console using Javascript?

查看:35
本文介绍了我可以使用Javascript将CSS样式属性打印到控制台吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有另一个基本问题,我似乎找不到在线答案.我可以使用javascript

Got another basic question that I couldn't seem to find the answer for online. I can change the CSS property of an element easily using javascript,

 document.getElementById("ExampleID").style.height="30px";

但是,每当我尝试使用

console.log(document.getElementById("ExampleID").style.height);

它将打印空白行而不是属性.如何打印所需元素的样式属性值?非常感谢

it prints a blank line instead of the property. How can I print a style property value of the desired element? Thank you very much

推荐答案

您可以使用 getComputedStyle

You can use 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将CSS样式属性打印到控制台吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆