欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS如何获取元素的不透明属性值?

程序员文章站 2024-03-17 12:17:40
...

元素的不透明属性存在IE为代表的filter属性和以firfox为代表的opacity属性,因此从兼容性考虑会获取到两种值。

IE与FF中对于不透明度的写法:

IE

filter:alpha(opacity=60);

FF

opacity:0.6;

 

JS获取元素不透明书属性的函数:

function getOpacity(obj)

{

    if(getComputedStyle(obj,null)['opacity'])

    {

        return getComputedStyle(obj,null)['opacity'];

    }

    else

    {

        return obj.currentStyle['fileter'];

    }

}

 

示例:

<!doctype html>
<html>
<head>
<title>运动</title>
<meta charset="utf-8">
<style>
.div1{width:100px;height:100px;float:left;background:#ccc;margin:5px;opacity:0.6;filter:alpha(opacity=60);}
</style>
<script>
function getOpacity(obj)

{

    if(getComputedStyle(obj,null)['opacity'])

    {

        return getComputedStyle(obj,null)['opacity'];

    }

    else

    {

        return obj.currentStyle['fileter'];

    }

}
window.onload=function()
{
	var oDiv0=document.getElementsByClassName('div1')[0];
	alert(getOpacity(oDiv0));
}

</script>
</head>
<body>
<div class="div1">0</div>
<div class="div1">1</div>
<div class="div1">2</div>
<div class="div1">3</div>
<div class="div1">4</div>
<div class="div1">5</div>
<div class="div1">6</div>
<div class="div1">7</div>
<div class="div1">8</div>
<div class="div1">9</div>
<div class="div1">10</div>
<div class="div1">11</div>
<div class="div1">12</div>
<div class="div1">13</div>
<div class="div1">14</div>
</body>
</html>