0

给出此错误 Uncaught TypeError: Cannot read property 'style' of null in the following code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>
<style>
#div{
    width:100px;
    height:100px;
    background:#999;
    -webkit-transform:none;
}
</style>
<script type="text/javascript">
deg1=0;
deg2=0;
function animando(){
    deg1++;
    deg2++;
    quadrado = document.getElementById('div');  
    quadrado.style.webkitTransform = "rotate(-2deg)";
    }
setInterval(animando(),100);
</script>
</head>

<body>
<div id="div">

</div>
</body>
</html>
4

2 回答 2

2
setInterval(animando(),100);

应该

setInterval(animando, 100);

没有(). 您正在尝试在代码中实际调用 animando 函数,因此当 setInterVal 调用准备其参数时,实际<div id="div">元素尚未被解析,因此 getElementById() 调用返回 null 因为 ID 不存在(然而)。

于 2013-07-16T19:49:01.643 回答
0
setInterval(animando(),100);

应该

setInterval(animando, 100);

没有(). 您正在尝试animando在代码中实际调用该函数,因此当setInterval调用准备其参数时,实际<div id="div">元素尚未被解析,因此getElementById()调用返回null,因为 ID 不存在(尚)。

于 2013-09-05T15:31:12.540 回答