【问题标题】:How do I get the opacity of an element using Javascript?如何使用 Javascript 获取元素的不透明度?
【发布时间】:2012-07-07 02:16:57
【问题描述】:

如果我有:

#em {
  opacity:0.5;
}

如何使用javascript 获得#em 的不透明度? :D

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

return document.getElementById("em").style.opacity;

【问题讨论】:

    标签: javascript css opacity


    【解决方案1】:
    document.getElementById("em").style.opacity;
    

    如果您使用内联样式,它将正常工作。例如。

    <div id="em" style="width: 50px; height: 50px; opacity: 0.5;">
    

    【讨论】:

    • 它的不透明度*顺便说一句
    【解决方案2】:
    var em = document.getElementById("em");
    var  temp = window.getComputedStyle(em).getPropertyValue("opacity");
    

    现在,变量 temp 的 opacity 值为“em”。

    【讨论】:

      【解决方案3】:

      在样式表中设置 CSS 值与通过 style 属性设置不同。您需要查看 getComputedStyle 方法来获取它(对于旧版 IE,还需要查看 currentStyle)。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 2011-05-10
      • 2012-01-01
      • 2011-01-22
      • 1970-01-01
      • 2011-11-13
      • 2018-03-09
      相关资源
      最近更新 更多