因为imgIndex=3的时候if(!fImgs[imgIndex]||imgIndex==showImgIndex)
return false;
停止循环了var fImgs = new Array();
fImgs[0] = {img:"2008929105038359.jpg",href:"2008102892246468.html"};
fImgs[1] = {img:"200892917107203.jpg",href:"2008102892246468.html"};
fImgs[2] = {img:"20081020143311125.jpg",href:"2008102892246468.html"};
var imgTimer;
var showImgIndex=-1;
function ShowImg(){
var _flash_div =document.getElementById("DivScroll");
//问题就出在这个判断这里,大小比较不了,都是直接跳过
//imgIndex 用户传入的参数,fImgs.length数组长度
if(showImgIndex>=fImgs.length)
showImgIndex=0;
if(!fImgs[showImgIndex])
return false;
var _liIndex ="_aImg"+showImgIndex;
var img =new Image();
img.border = "0";
img.src = fImgs[showImgIndex].img;
img.width = "173";
img.height = "131";
img.className="jpadpic";
var _a =document.createElement("a");
_a.setAttribute("target","_blank");
_a.setAttribute("id",_liIndex);
_a.setAttribute("href",fImgs[showImgIndex].href);
_a.appendChild(img);
_flash_div.innerHTML="";
_flash_div.appendChild(_a); return true;
}function imagePlayer()
{
debugger;
if(imgTimer)return;
if(showImgIndex>fImgs.length)
{
showImgIndex =-1;
}
ShowImg(++showImgIndex);
imgTimer =setInterval(function(){var stat = ShowImg(++showImgIndex);if(!stat){stop();}},3000);
}function stop()
{
clearInterval(imgTimer);
imgTimer = null;
}//function:get element by its Id
function $(e)
{
return document.getElementById ? document.getElementById(e) : null;
}
return false;
停止循环了var fImgs = new Array();
fImgs[0] = {img:"2008929105038359.jpg",href:"2008102892246468.html"};
fImgs[1] = {img:"200892917107203.jpg",href:"2008102892246468.html"};
fImgs[2] = {img:"20081020143311125.jpg",href:"2008102892246468.html"};
var imgTimer;
var showImgIndex=-1;
function ShowImg(){
var _flash_div =document.getElementById("DivScroll");
//问题就出在这个判断这里,大小比较不了,都是直接跳过
//imgIndex 用户传入的参数,fImgs.length数组长度
if(showImgIndex>=fImgs.length)
showImgIndex=0;
if(!fImgs[showImgIndex])
return false;
var _liIndex ="_aImg"+showImgIndex;
var img =new Image();
img.border = "0";
img.src = fImgs[showImgIndex].img;
img.width = "173";
img.height = "131";
img.className="jpadpic";
var _a =document.createElement("a");
_a.setAttribute("target","_blank");
_a.setAttribute("id",_liIndex);
_a.setAttribute("href",fImgs[showImgIndex].href);
_a.appendChild(img);
_flash_div.innerHTML="";
_flash_div.appendChild(_a); return true;
}function imagePlayer()
{
debugger;
if(imgTimer)return;
if(showImgIndex>fImgs.length)
{
showImgIndex =-1;
}
ShowImg(++showImgIndex);
imgTimer =setInterval(function(){var stat = ShowImg(++showImgIndex);if(!stat){stop();}},3000);
}function stop()
{
clearInterval(imgTimer);
imgTimer = null;
}//function:get element by its Id
function $(e)
{
return document.getElementById ? document.getElementById(e) : null;
}
imgIndex 应该是你上面数组的索引值吧??
fImgs.length 就是数组的长度.我想不出什么情况下 索引值会大于数组的长度,如果出现,很明显是出错了.
正常来说, 代码每次都跳过是正确的.
alert(fImgs.length)
alert(imgIndex>fImgs.length)
if(imgIndex>fImgs.length){
imgIndex=0;
}
alert(imgIndex)
数组长度为2,当imgIndex=3时,执行到该判断的时候
if(imgIndex>fImgs.length)//(3 > 2){imgIndex=0;}
imgIndex=0;
因为进行大小比较时不成功,而没有将imgIndex重置为0的原因。
我要问的其实是这个,请看清楚我的测试过程,谢谢你的回答...
imgIndex=0;
//当imgIndex==fImgs.length时已经超过数组的最大索引了,所以!fImgs[imgIndex]就是true了
if(!fImgs[imgIndex]||imgIndex==showImgIndex)
return false;
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Test</title>
<script type="text/javascript">
var fImgs = new Array();
fImgs[0] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/2008929105038359.jpg",href:"2008102892246468.html"};
fImgs[1] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/200892917107203.jpg",href:"2008102892246468.html"};
fImgs[2] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/20081020143311125.jpg",href:"2008102892246468.html"};
var imgTimer;
var showImgIndex=-1;function ShowImg(imgIndex){
var _flash_div =document.getElementById("DivScroll");
if(imgIndex>fImgs.length)
imgIndex=0;
if(!fImgs[imgIndex]||imgIndex==showImgIndex)
return false;
var _liIndex ="_aImg"+imgIndex;
var img =new Image();
img.border = "0";
img.src = fImgs[imgIndex].img;
img.width = "173";
img.height = "131";
img.className="jpadpic";
var _a =document.createElement("a");
_a.setAttribute("target","_blank");
_a.setAttribute("id",_liIndex);
_a.setAttribute("href",fImgs[imgIndex].href);
_a.appendChild(img);
_flash_div.innerHTML="";
_flash_div.appendChild(_a);
showImgIndex =imgIndex;
return true;
}function imagePlayer()
{
if(imgTimer)return;
if(showImgIndex>fImgs.length)
{
showImgIndex =-1;
}
ShowImg(showImgIndex+1);
imgTimer =setInterval(function(){var stat = ShowImg(showImgIndex+1);if(!stat){stop();}},3000);
}function stop()
{
clearInterval(imgTimer);
imgTimer = null;
}//function:get element by its Id
function $(e)
{
return document.getElementById ? document.getElementById(e) : null;
}
</script>
</head><body onload="imagePlayer();">
<div class="box" >
<div class="neirong">
<div class="neirongr">
<!-- 精品招商 -->
<div class="jpad">
<div class="no">
<ul>
<li><a id="_lia1" href="javascript:void(0);" onclick="ShowImg('0');" >[1]</a></li>
<li><a id="_lia2" href="javascript:void(0);" onclick="ShowImg('1');" >[2]</a></li>
<li><a id="_lia3" href="javascript:void(0);" onclick="ShowImg('2');" >[3]</a></li>
</ul>
</div>
<div id="DivScroll"></div>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</body>
</html>
你要给imgIndex赋值的话,要在函数开始用
var imgIndex = imgIndex;
<!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=gb2312" />
<title>Test</title>
<script type="text/javascript">
var fImgs = new Array();
fImgs[0] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/2008929105038359.jpg",href:"2008102892246468.html"};
fImgs[1] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/200892917107203.jpg",href:"2008102892246468.html"};
fImgs[2] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/20081020143311125.jpg",href:"2008102892246468.html"};
var imgTimer;
var showImgIndex=-1;function ShowImg(imgIndex){
var _flash_div =document.getElementById("DivScroll");
if(imgIndex>fImgs.length-1)
imgIndex=0;
if(!fImgs[imgIndex]||imgIndex==showImgIndex)
return false;
var _liIndex ="_aImg"+imgIndex;
var img =new Image();
img.border = "0";
img.src = fImgs[imgIndex].img;
img.width = "173";
img.height = "131";
img.className="jpadpic";
var _a =document.createElement("a");
_a.setAttribute("target","_blank");
_a.setAttribute("id",_liIndex);
_a.setAttribute("href",fImgs[imgIndex].href);
_a.appendChild(img);
_flash_div.innerHTML="";
_flash_div.appendChild(_a);
showImgIndex =imgIndex;
return true;
}function imagePlayer()
{
if(imgTimer)return;
if(showImgIndex>fImgs.length)
{
showImgIndex =-1;
}
ShowImg(showImgIndex+1);
imgTimer =setInterval(function(){var stat = ShowImg(showImgIndex+1);if(!stat){stop();}},3000);
}function stop()
{
clearInterval(imgTimer);
imgTimer = null;
}//function:get element by its Id
function $(e)
{
return document.getElementById ? document.getElementById(e) : null;
}
</script>
</head><body onload="imagePlayer();">
<div class="box" >
<div class="neirong">
<div class="neirongr">
<!-- 精品招商 -->
<div class="jpad">
<div class="no">
<ul>
<li><a id="_lia1" href="javascript:void(0);" onclick="ShowImg('0');" >[1]</a></li>
<li><a id="_lia2" href="javascript:void(0);" onclick="ShowImg('1');" >[2]</a></li>
<li><a id="_lia3" href="javascript:void(0);" onclick="ShowImg('2');" >[3]</a></li>
</ul>
</div>
<div id="DivScroll"></div>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</body>
</html>
if(imgIndex>fImgs.length-1)
imgIndex=0;
if(imgIndex >= fImgs.length)
imgIndex=0;
就ok了
其实我没认真看数组(数组是copy来的),调试的时候即时窗口中给的是2,就把他当2看了,所以当使用4楼的给的意见的时候,就知道自己错了
呵呵.