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

js菜鸟求解?图片自动切换问题,
图片自动切换有问题,从第一个一下就切换到最后,中间两个没播放,代码如下:
<!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>
  <title></title>
  // <link rel="Stylesheet" type="text/css" href="lunbot.css"/>
  <script type="text/javascript" src="lunbo.js"></script>
</head>
<body>
  <div id="a">
  <ul>
  <li class="b">1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  </ul>
  <div id="c">
  <img id="img" src="imgs/p1.jpg" />
  </div>
  </div> 
</body>
</html>
js代码如下:
var imgArry = ["imgs/p1.jpg", "imgs/p2.jpg", "imgs/p3.jpg", "imgs/p4.jpg"];
var img1;
var i;
window.onload = function () {
  img1 = document.getElementById("img");
  setInterval(show, 2000);
function show()
 {
  for (i = 0; i < imgArry.length; i++)
  {
  img1.setAttribute("src", imgArry[i]);
  }
  }

}; 


------解决方案--------------------
[code=JSscrpt]
var arr=new Array();
arr[0]="imgs/p1.jpg";//放图片地址
arr[1]="imgs/p2.jpg";
arr[2]="imgs/p3.jpg";
arr[3]="imgs/p4.jpg";
var num=0;
setInterval(turnpic,4000); //每隔4秒转换图片
function turnpic(){
idsrc=document.getElementById("img");
if(num==arr.length-1)
    num=0;
else
    num+=1;
idsrc.src=arr[num];
}

[/code]
------解决方案--------------------
JScript code

var arr=new Array(); 
arr[0]="imgs/p1.jpg";//放图片地址 
arr[1]="imgs/p2.jpg"; 
arr[2]="imgs/p3.jpg"; 
arr[3]="imgs/p4.jpg"; 
var num=0; 
setInterval(turnpic,4000); //每隔4秒转换图片 
function turnpic(){ 
  idsrc=document.getElementById("img"); 
  if(num==arr.length-1) 
        num=0; 
  else 
        num+=1; 
  idsrc.src=arr[num]; 
}

------解决方案--------------------
for (i = 0; i < imgArry.length; i++)
{
img1.setAttribute("src", imgArry[i]);
},
到头了自然就不跑了,i == imgarry.length-1的时候i-1就可以了
------解决方案--------------------
var imgArry = ["imgs/p1.jpg", "imgs/p2.jpg", "imgs/p3.jpg", "imgs/p4.jpg"];
var img1;
var i;
window.onload = function () {
img1 = document.getElementById("img");
setInterval(show, 2000);
function show()
 {
img1.setAttribute("src", imgArry[i]);
i++
}

};

------解决方案--------------------
应该是这样

var imgArry = ["imgs/p1.jpg", "imgs/p2.jpg", "imgs/p3.jpg", "imgs/p4.jpg"];
var img1;
var i;
window.onload = function () {
img1 = document.getElementById("img");
setInterval(show, 2000);
function show()
 {
img1.setAttribute("src", imgArry[i]);
i++

if(i==imgArry.length)i = 0;
}