这是一个非常基本的问题:为什么下面代码中的finishLoading()函数无法访问#myStyle CSS选择器的'opacity'属性?警报不会显示任何内容,我已经确认"不透明度"属性为"false".
非常感谢!
<html>
<head>
<style type="text/css">
<!--
#myStyle
{
opacity: 0.50;
}
-->
</style>
<script type="text/javascript">
<!--
function finishedLoading()
{
alert(document.getElementById('myStyle').style.opacity);
}
-->
</script>
</head>
<body onload="finishedLoading();">
<div id="myStyle">
hello
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
您只能在计算后才能获取通过类设置的值.
var oElm = document.getElementById ( "myStyle" );
var strValue = "";
if(document.defaultView && document.defaultView.getComputedStyle)
{
strValue = document.defaultView.getComputedStyle(oElm, null).getPropertyValue("-moz-opacity");
}
else if(oElm.currentStyle) // For IE
{
strValue = oElm.currentStyle["opacity"];
}
alert ( strValue );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4000 次 |
| 最近记录: |