object.style.x不返回任何内容 [英] object.style.x doesn't return anything
问题描述
在我的网站上的JavaScript我有这样的:
In JavaScript on my website I have something like this:
console.log(document.getElementById("side_news").style.display);
我已经尝试过很多样式,它不返回任何东西,只是空白。
and I have tried this with a lot of styles and it doesn't return anything, just blank. What am I doing wrong?
推荐答案
大多数元素在通过访问时不会显示所有属性, object.style
。 div
元素的默认显示样式为 block
,但通过 style
将产生一个空值。
Most elements don't show all of their attributes when accessing through object.style
. A div
element has a default display style of block
but accessing it through style
will result an empty value.
一个解决方案是使用 getComputedStyle
- 如果不支持由浏览器, currentStyle
。
A solution is to use getComputedStyle
- or, if not supported by the browser, currentStyle
.
if (window.getComputedStyle)
status = window.getComputedStyle(targetElement, null);
else
status = targetElement.currentStyle;
这将显示所有css更改的元素样式。
This will show the element's style with all the css changes.
这篇关于object.style.x不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!