图片浏览_添加事件监听

<html>
  <head>
    <title></title>
  <head>
   <script>
     window.onload=function(){
         var imgs=new Array("a.jpg","b.jpg","c.jpg");
         var foot=1;
         //获取按钮
         var preButton=document.getElementById("preButton");
         var nextButton=document.getElementById("nextButton");
         //获取img
         var myimg=document.getElementById("myimg");
         //为按钮添加事件监听
         preButton.addEventListener("click",function(){
            if(foot<=0){
                foot=imgs.length-1;
            }
            myimg.src="d:/picture/"+imgs[foot--];
            //alert(myimg.src);
         },false);

       nextButton.addEventListener("click",function(){
              if(foot>=imgs.length){
                    foot=0;
              }
              myimg.src="d:/picture/"+imgs[foot++];
          // alert(myimg.src);
       },false);
  
     }
   </script>
  <body>
     <span id="myspan">
       <img id="myimg" src="d:/picture/a.jpg" height="60%" />
     </span>
     <div>
        <button type="button" id="preButton">上一张</button>
        <button type="button" id="nextButton">下一张</button>
     <div> 
  </body>
</html>

 

posted @ 2017-09-10 15:49  scwyfy  阅读(298)  评论(0编辑  收藏  举报