在页面加载时获取javascript中的css属性
我试图在页面加载时获得css属性,但正如您在下面看到的,它不会显示显示,但如果我更改它,它会正确显示。我的问题是如何在显示器1上显示“块” 谢谢你的帮助在页面加载时获取javascript中的css属性,javascript,css,Javascript,Css,我试图在页面加载时获得css属性,但正如您在下面看到的,它不会显示显示,但如果我更改它,它会正确显示。我的问题是如何在显示器1上显示“块” 谢谢你的帮助 let hello=document.getElementById('hello'); document.getElementById('you').innerHTML+=hello.style.display; hello.style.display=“内联块”; document.getElementById('too').innerHT
let hello=document.getElementById('hello');
document.getElementById('you').innerHTML+=hello.style.display;
hello.style.display=“内联块”;
document.getElementById('too').innerHTML+=hello.style.display
你好{
背景颜色:黄色;
高度:20px;
显示:块;
}
你好
显示1:
显示2:
.style
读取元素的内联样式(如
中所示),而不是样式表。可以使用hello.getBoundingClientRect().height
读取DOM元素的高度
或者,您可以使用getComputedStyle
:
const styles=window.getComputedStyle(您好)
const height=styles.getPropertyValue('height'))
.style
读取元素的内联样式(如在
中),而不是样式表。可以使用hello.getBoundingClientRect().height
读取DOM元素的高度
或者,您可以使用getComputedStyle
:
const styles=window.getComputedStyle(您好)
const height=styles.getPropertyValue('height'))
我需要获取显示值,而你的getBoundingClientRect()不适用,因此我编辑了我的帖子getComputedStyle就是我需要的,谢谢:)我需要获取显示值,而你的getBoundingClientRect()不适用,所以我编辑了我的帖子getComputedStyle就是我需要的,谢谢:)