日期:2014-05-16  浏览次数:20416 次

实现两张图片切换,为什么下面这段代码实现不了,有没有更好的办法
<head>
  <title></title>
  <script type="text/javascript" language="javascript">
   
  function chgImg(){
  var current_id = 0;
  var the_img=window.document.getElementById("myImg");
  the_img.src ="images/pic1.jpg";
  current_id++;
  setTimeout("the_img.src = images/pic2.jpg",1000);
  }
  }
  setInterval("chgImg()",1000);
  </script>
</head>
<body onload="chgImg()">
  <div>
  <img src="images/pic1.jpg" alt="" id="myImg" />
  </div>
</body>

------解决方案--------------------
HTML 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>无标题文档</title>
<script type="text/javascript">
function chgImg() {
    var o = document.getElementById('myImg');
    var src = o.src;
    o.src = o.getAttribute('src2');
    o.setAttribute('src2', src);
}
window.onload = function() {
    window.setInterval('chgImg()', 1000);
}
</script>
</head>

<body>
<img src="images/pic1.jpg" src2="images/pic2.jpg" id="myImg" />
</body>
</html>