Javascript object.style.x不';我什么也不退

Javascript object.style.x不';我什么也不退,javascript,css,Javascript,Css,在我网站上的JavaScript中,我有如下内容: console.log(document.getElementById("side_news").style.display); 我已经尝试了很多样式,但它没有返回任何内容,只是空白。我做错了什么?尝试使用getComputedStyle(): MDN文档:大多数元素在通过对象.style访问时不会显示其所有属性。div元素的默认显示样式为block,但通过style访问它将导致一个空值 解决方案是使用getComputedStyle——或者

在我网站上的JavaScript中,我有如下内容:

console.log(document.getElementById("side_news").style.display);

我已经尝试了很多样式,但它没有返回任何内容,只是空白。我做错了什么?

尝试使用
getComputedStyle()


MDN文档:

大多数元素在通过
对象.style
访问时不会显示其所有属性。
div
元素的默认显示样式为
block
,但通过
style
访问它将导致一个空值

解决方案是使用
getComputedStyle
——或者,如果浏览器不支持,
currentStyle

if (window.getComputedStyle)
    status = window.getComputedStyle(targetElement, null);
else
    status = targetElement.currentStyle;

这将显示元素的样式和所有css更改。

Do
console.log(document.getElementById(“side_news”))
并查看是否得到除
null
以外的任何内容。如果为null,他将得到一个关于无法访问null的style属性的错误。
。style
仅访问内联样式,不是从CSS文件设置的样式。哦,我不知道。。。谢谢GetComputedStyle->这很有效。非常感谢。
if (window.getComputedStyle)
    status = window.getComputedStyle(targetElement, null);
else
    status = targetElement.currentStyle;