为什么用class不行,用id就可以了?
<!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>无标题文档</title> <style type="text/css"> .box{ width:100px; height:100px; background:#CF6; border:2px solid; position:relative; left:-50px; } </style> </head> <body> <div class="box"> </div> <script type="text/javascript"> window.onload=function(){ var box=document.getElementsByClassName('box'); box.onmouseover=function(){ startMove(); } } function startMove(){ setInterval(function(){ var box=document.getElementsByClassName('box'); box.style.left=box.offsetLeft+10+'px'; },30) } </script> </body> </html>
上面的代码没有效果,如果把box改成id的就可以了,这是为什么呢