美好的一天!老实说,问题有点复杂。我想知道为什么以下代码行不起作用:
pic.style.opacity = toString((5-z)/10);
为什么我认为它应该有效?
- opacity 成员变量是一个字符串,所以它必须是一个字符串,因此只要在系统计算表达式后存在“toString”方法,它就应该变成一个字符串,因此它应该是可行的。<
注意:z 是一个整数,其值从 0 到 4 不等。
如果有人想自己测试:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<style>
#asd{
background-image : url(fry.jpg);
margin-left:20%;
margin-top:20%;
height:100px;
width:100px;
}
</style>
</head>
<body>
<div id="asd"></div>
</body>
</html>
<script
var z = 3;
function foo(){
document.getElementById("asd").style.opacity = toString((5-z)/10);
}
foo();
</script>
最佳答案
你不需要toString
。
只是做:
document.getElementById("asd").style.opacity = (5-z)/10;
无论如何,您没有正确使用 toString
。如果您想在任何地方使用它,请这样做:
var number = 42.0;
var string = number.toString(10);
10
是可选的和默认的。例如,您可以使用 2
在双系统中进行转换。
关于javascript - 使用javascript设置图像不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17347215/