我有一个动画不透明的脚本,但如果以前没有设置不透明度,那么它就会消失。我只是想知道是否有办法检查是否已设置不透明度。提前谢谢!
答案 0 :(得分:1)
通常,未设置的CSS属性在JavaScript中返回空值。
更新:element.style
似乎只会返回之前通过内联样式中指定的JavaScript 或设置的值。要获得CSS属性的真实值,您需要使用元素的所谓“计算样式”。
这是一个执行此操作的函数(从quirksmode复制而来):
function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
return y;
}
但是,如果尚未设置不透明度,这将返回1.0
(默认值),因此实际上无法确定是否在CSS中实际定义了不透明度。