一段小代码运行时报错Uncaught TypeError: Cannot read property 'style' of null 。恳请大神解答

cantiaozi 2018-08-02 05:16:59
这一段小代码在运行时报错:Uncaught TypeError: Cannot read property 'style' of null。
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>IFE ECMAScript</title>

</head>
<body>
<div id="fade-obj" style="width:300px;height:300px;background:rgb(0, 0, 0, 1)"></div>
<button id="fade-btn">淡出</button>
<script>
var button = document.getElementById("fade-btn");
var obj = document.getElementById("fade-boj");
var index = 0;
var transParent;
var intervalId;
function fadeOut() {
if (index === 1000) {
clearInterval(intervalId);
}
transParent = 1 - index / 1000;
console.log("sssssss")
console.log("style", obj.style);
obj.style.background = "rgb(0, 0, 0," + " " + transParent + ")";
index ++;
}
button.onclick = function() {

if(button.firstChild.nodeValue === "淡出") {
button.disabled = true;
intervalId = setInterval(fadeOut, 10);
}
}
</script>
</body>
</html>


跪求大神解答!谢谢
...全文
888 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
天际的海浪 2018-08-02
  • 打赏
  • 举报
回复
一是 obj写成了boj
var obj = document.getElementById("fade-obj");

二是有透明度的应该是rgba()
rgba(0, 0, 0, 1)
"rgba(0, 0, 0," + " " + transParent + ")";

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧