问答详情
源自:1-2 案例HTML+CSS结构

ncaught TypeError: Cannot read property 'getElementsByTagName' of null

window.onload= function(){
   //容器对象
   var box=document.getElementById("container");
   var imgs=box.getElementsByTagName("img");

   //获得单张图片的宽度
   var imgWidth=imgs[0].offsetWidth;

   //设置掩藏门体露出的宽度

   var exposeWidth=160;

   //设置容器总宽度
   var boxWidth=imgWidth+(imgs.length-1)*exposeWidth;
   box.style.width=boxWidth+"px";

   //设置每到门的初始位置
   for(var i=1,len=imgs.length;i<len;i++){
       imgs[i].style.left=imgWidth+exposeWidth*(i-1)+"px";
   }
}






HTML:

<div class="container">

       <img src="pic/风景1.jpg" alt="风景1" title="1"/>
       <img src="pic/风景2.jpg" alt="风景2" title="2"/>
       <img src="pic/风景3.jpg" alt="风景3" title="3"/>
       <img src="pic/风景4.jpg" alt="风景4" title="4"/>

</div>


找了半天没有找打错误在哪里了,请各位高手帮忙


提问者:慕粉3503008 2016-10-20 16:43

个回答

  • 慕粉3503008
    2016-10-20 16:45:25

    好囧。。。。。知道错在那里了