美好的一天!诚实地说,这个问题有点复杂。我想知道为什么下面的代码行不起作用:用javascript设置图像不透明度
pic.style.opacity = toString((5-z)/10);
为什么我认为它应该工作?
- 不透明构件变量是一个字符串,它必须是一个字符串,因此只要“的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(); pic.style.opacity =((5-z)/ 10);另外,.toString()以相反的方式工作,((5-z)/ 10).toString() – frenchie