Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t访问CSS选择器';来自Javascript的属性_Javascript_Html_Css - Fatal编程技术网

Can';t访问CSS选择器';来自Javascript的属性

Can';t访问CSS选择器';来自Javascript的属性,javascript,html,css,Javascript,Html,Css,这里有一个非常基本的问题:为什么下面代码中的finishLoading()函数不能访问#myStyle CSS选择器的“opacity”属性?警报没有显示任何内容,我已验证“不透明”属性为“假” 非常感谢 <html> <head> <style type="text/css"> <!-- #myStyle { opacity: 0.50; } --> </style> <script type="text/javasc

这里有一个非常基本的问题:为什么下面代码中的finishLoading()函数不能访问#myStyle CSS选择器的“opacity”属性?警报没有显示任何内容,我已验证“不透明”属性为“假”

非常感谢

<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>

你好

我建议你看一看和上的一些帖子,这样做很容易。

不透明度应该是一个数字,而不是布尔值。它在任何其他浏览器中工作吗?

此链接帮助


不透明度适用于Mozilla和Safari,过滤器适用于Explorer。值的范围从0到10。

只有在计算后,才能通过类获得设置的值

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 );

问题是,
element.style.opacity
仅存储在元素的
style
属性内设置的值。如果要访问来自其他样式表的样式值,请查看


干杯,

e.g.$(“#myStyle”).css(“不透明”,“0.8”)虽然我很乐意使用jQuery或其他任何可以完成任务的工具,但我想了解我所缺少的解释我所看到的行为的概念。也许你可以告诉我我应该研究HTML/CSS/Javascript的哪些特定领域?jQuery很酷,但它只是phoenix在回答中解释的函数的抽象。啊,对不起,我不清楚。在尝试访问“不透明度”属性时,我希望使用浮点/数字,但该值似乎不存在。例如:如果(!document.getElementById('myStyle').style.opacity)警报('Aw-oh!');此代码始终在Safari和Firefox中显示警报。我想我在这里遗漏了一些基本概念,我只是不确定它是什么,或者我可以做什么来访问“不透明”属性。谢谢你的回复!啊,是的,或者采取凤凰城的方式。啊哈!非常感谢您的链接和解释。现在很清楚了。然而,这不仅仅是不透明度的情况,其他所有样式属性也是如此。尝试添加“颜色:红色;”到您的样式表,并向您的事件处理程序发出警报(document.getElementById('myStyle').style.color)。注释规则!currentStyle以IE的方式处理,其他任何人都支持getComputedStyle()。document.defaultView是获取负责呈现文档的
窗口的一种方法。顺便说一句,
-moz不透明性
将不再起作用(自FF 2以来,在Opera和WebKit中它从未起过作用)。。。。我应该添加一个简单的“不透明度”而不是“-moz opacity”。但是不应用不透明度。我想再次阅读这篇伟大的文章:关于使用此函数可能引起的一般问题。
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 );