请问这个代码错在那里了?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<!-- saved from url=(0026)http://127.0.0.1/photo.asp -->
<HTML> <HEAD>
<META http-equiv=Content-Type content= "text/html; charset=gb2312 ">
<SCRIPT language=JavaScript1.2>
var time1 = 0; //打开页面时等待图片载入的时间,单位为秒,可以设置为0
var time2 = 5; //图片轮转的间隔时间
var timeout1 = time1*1000;
var timeout2 = time2*1000;
var nn;
nn=1;//初始焦点
var curFileNum = 1;//传递给myPlayer对象 表示目前焦点序列值
if(navigator.appName == "Microsoft Internet Explorer "){
setTimeout( 'change_img() ',timeout1);
}
function change_img(){
if(nn> 5) nn=1
setTimeout( 'setFocus2( '+nn+ ') ',timeout1);
nn++;
tt=setTimeout( 'change_img() ',timeout2);
}
function setFocus2(i){
curFileNum = i;
selectLayer1(i);
}
function setFocus1(i){
nn = i;
curFileNum = i;
selectLayer1(i);
}
function selectLayer1(i){
switch(i){
case 1:
document.getElementById( "focusPic1 ").style.display= "block ";
document.getElementById( "focusPic2 ").style.display= "none ";
document.getElementById( "focusPic3 ").style.display= "none ";
document.getElementById( "focusPic4 ").style.display= "none ";
document.getElementById( "focusPic5 ").style.display= "none ";
break;
case 2:
document.getElementById( "focusPic1 ").style.display= "none ";
document.getElementById( "focusPic2 ").style.display= "block ";
document.getElementById( "focusPic3 ").style.display= "none ";
document.getElementById( "focusPic4 ").style.display= "none ";
document.getElementById( "focusPic5 ").style.display= "none ";
break;
case 3:
document.getElementById( "focusPic1 ").style.display= "none ";
document.getElementById( "focusPic2 ").style.display= "none ";
document.getElementById( "focusPic3 ").style.display= "block ";
document.getElementById( "focusPic4 ").style.display= "none ";
document.getElementById( "focusPic5 ").style.display= "none ";
break;
case 4:
document.getElementById( "focusPic1 ").style.display= "none ";
document.getElementById( "focusPic2 ").style.display= "none ";
document.getElementById( "focusPic3 ").style.display= "none ";
document.getElementById( "focusPic4 ").style.display= "block ";
document.getElementById( "focusPic5 ").style.display= "none ";
break;
case 5:
document.getElementById( "focusPic1 ").style.display= "none ";
document.getElementById( "focusPic2 ").style.display= "none ";
document.getElementById( "focusPic3 ").style.display= "none ";
document.getElementById( "focusPic4 ").style.display= "none ";
document.getElementById( "focusPic5 ").style.display= "block ";
break;
}
}
</SCRIPT>
<STYLE> .focusPicNum {
Z-INDEX: 99; LEFT: 680px; WIDTH: 149px; POSITION: absolute; TOP: 350px; HEIGHT: 23px
}
.focusPicNum IMG {
MARGIN: 3px
}
</STYLE>
<META content= "MSHTML 6.00.3790.2858 " name=GENERATOR> </HEAD>
<BODY>
<DIV class=focusPic id=focusPic1>
<DIV class=focusPicNum> <IMG height=15 alt=第一张 src= "photo_files/01on.gif "
width=23 border=0> <A href= "javascript:setFocus1(2); "> <IMG height=15 alt=第二张
src= "photo_files/02off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(3); "> <IMG height=15 alt=第三张
src= "photo_files/03off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(4); "> <IMG height=15 alt=第四张
src= "photo_files/04off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(5); "> <IMG height=15 alt=第五张
src= "photo_files/05off.gif " width=23 border=0> </A> </DIV>
<DIV class=focusPicImg> <A href= "http://127.0.0.1/ " target=_blank> <IMG title= " "
height=190 src= "photo_files/200741920565233333.jpg " width=545
border=0> </A> </DIV> </DIV>
<DIV class=focusPic id=focusPic2 style= "DISPLAY: none ">
<DIV class=focusPicNum> <A href= "javascript:setFocus1(1); "> <IMG height=15 alt=第一张
src= "photo_files/01off.gif " width=23 border=0> </A> <IMG height=15 alt=第二张
src= "photo_files/02on.gif " width=23 border=0> <A
href= "javascript:setFocus1(3); "> <IMG height=15 alt=第三张
src= "photo_files/03off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(4); "> <IMG height=15 alt=第四张
src= "photo_files/04off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(5); "> <IMG height=15 alt=第五张
src= "photo_files/05off.gif " width=23 border=0> </A> </DIV>
<DIV class=focusPicImg> <A href= "http://127.0.0.1/ " target=_blank> <IMG title= " "
height=190 src= "photo_files/200741920564333333.jpg " width=545
border=0> </A> </DIV> </DIV>
<DIV class=focusPic id=focusPic3 style= "DISPLAY: none ">
<DIV class=focusPicNum> <A href= "javascript:setFocus1(1); "> <IMG height=15 alt=第一张
src= "photo_files/01off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(2); "> <IMG height=15 alt=第二张
src= "photo_files/02off.gif " width=23 border=0> </A> <IMG height=15 alt=第三张
src= "photo_files/03on.gif " width=23 border=0> <A
href= "javascript:setFocus1(4); "> <IMG height=15 alt=第四张
src= "photo_files/04off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(5); "> <IMG height=15 alt=第五张
src= "photo_files/05off.gif " width=23 border=0> </A> </DIV>
<DIV class=focusPicImg> <A href= "http://127.0.0.1/ " target=_blank> <IMG title= " "
height=190 src= "photo_files/200741920563533333.jpg " width=545
border=0> </A> </DIV> </DIV>
<DIV class=focusPic id=focusPic4 style= "DISPLAY: none ">
<DIV class=focusPicNum> <A href= "javascript:setFocus1(1); "> <IMG height=15 alt=第一张
src= "photo_files/01off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(2); "> <IMG height=15 alt=第二张
src= "photo_files/02off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(3); "> <IMG height=15 alt=第三张
src= "photo_files/03off.gif " width=23 border=0> </A> <IMG height=15 alt=第四张
src= "photo_files/04on.gif " width=23 border=0> <A
href= "javascript:setFocus1(5); "> <IMG height=15 alt=第五张
src= "photo_files/05off.gif " width=23 border=0> </A> </DIV>
<DIV class=focusPicImg> <A href= "http://127.0.0.1/ " target=_blank> <IMG title= " "
height=190 src= "photo_files/200741913361933333.jpg " width=545
border=0> </A> </DIV> </DIV>
<DIV class=focusPic id=focusPic5 style= "DISPLAY: none ">
<DIV class=focusPicNum> <A href= "javascript:setFocus1(1); "> <IMG height=15 alt=第一张
src= "photo_files/01off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(2); "> <IMG height=15 alt=第二张
src= "photo_files/02off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(3); "> <IMG height=15 alt=第三张
src= "photo_files/03off.gif " width=23 border=0> </A> <A
href= "javascript:setFocus1(4); "> <IMG height=15 alt=第四张
src= "photo_files/04off.gif " width=23 border=0> </A> <IMG height=15 alt=第五张
src= "photo_files/05on.gif " width=23 border=0> </DIV>
<DIV class=focusPicImg> <A href= "http://127.0.0.1/ " target=_blank> <IMG title= " "
height=190 src= "photo_files/200741716553zs.jpg " width=545
border=0> </A> </DIV> </DIV> </BODY> </HTML>
---------------------------------------------
把上面的代码保存一下 就可以看到错误了, 刷新有时候有错误 有时候没有。。。晕了。谢谢~
[解决办法]
js代码应该在整个页面加载完毕后再执行,一般的做法,是js放到页面的最后,或者在 <body onload= "xx() "> 在unload事件离开始执行代码。
出现错误一般是对象没有生成就开始调用造成的,刷新没有错误,是因为缓存