如何使用Javascript获取元素的不透明度?

Joh*_*ohn 10 javascript css opacity

如果我有:

#em {
  opacity:0.5;
}
Run Code Online (Sandbox Code Playgroud)

如何#em使用不透明度javascript?:d

我遇到了以下麻烦(它什么都不返回):

return document.getElementById("em").style.opacity;
Run Code Online (Sandbox Code Playgroud)

Gra*_*ham 9

在样式表中设置CSS值与通过style属性设置它不同.您需要查看getComputedStyle获取此方法的方法(以及currentStyle旧IE).


小智 7

var em = document.getElementById("em");
var  temp = window.getComputedStyle(em).getPropertyValue("opacity");
Run Code Online (Sandbox Code Playgroud)

现在,变量temp的不透明度值为“ em”。