我是Javascript的新手,并且这个代码段一次正常运行。它用于背景图像滑块。我无法弄清楚导致此错误的原因。代码如下:
<script type="text/javascript">
window.onload=startslideshow();
var words = [displayimagetwo, displayimagethree, displayimagefour, displayimageone];
var x = 0;
var looptimer;
// (2) define the delayed loop function
function delayedLoop(){
looptimer = setTimeout(function(){
words[x]();
x++;
if (x >= 4) {
x=0;
}
if (x < 4) {
delayedLoop();
}
}, 5000)
}
function displayimageone(){
imgO = document.getElementById("image-one").getElementsByTagName('img');
var str = '';
for(i=0; i < imgO.length; i++){
str += imgO[i].src+"\n";
imageone=str;
// alert(imageone);
document.getElementById("main-image").style.background = "url(" + imageone + ") no- repeat";
document.getElementById("main-image").style.backgroundSize = "cover";
}
}
function displayimagetwo(){
imgO = document.getElementById("image-two").getElementsByTagName('img');
var str = '';
for(i=0; i < imgO.length; i++){
str += imgO[i].src+"\n";
imagetwo=str;
// alert(imagetwo);
document.getElementById("main-image").style.background = "url(" + imagetwo + ") no- repeat";
document.getElementById("main-image").style.backgroundSize = "cover";
}
}
function displayimagethree(){
imgO = document.getElementById("image-three").getElementsByTagName('img');
var str = '';
for(i=0; i < imgO.length; i++){
str += imgO[i].src+"\n";
imagethree=str;
// alert(imagethree);
document.getElementById("main-image").style.background = "url(" + imagethree + ") no- repeat";
document.getElementById("main-image").style.backgroundSize = "cover";
}
}
function displayimagefour(){
imgO = document.getElementById("image-four").getElementsByTagName('img');
var str = '';
for(i=0; i < imgO.length; i++){
str += imgO[i].src+"\n";
imagefour=str;
// alert(imagefour);
document.getElementById("main-image").style.background = "url(" + imagefour + ") no- repeat";
document.getElementById("main-image").style.backgroundSize = "cover";
}
}
function clickimageone(){
clearTimeout(looptimer);
displayimageone();
}
function clickimagetwo(){
clearTimeout(looptimer);
displayimagetwo();
}
function clickimagethree(){
clearTimeout(looptimer);
displayimagethree();
}
function clickimagefour(){
clearTimeout(looptimer);
displayimagefour();
}
function startslideshow(){
delayedLoop();
displayimageone();
}
</script>
我收到的错误说正确的是这行代码:
for(i=0; i < imgO.length; i++){
我正在处理的页面是http://info.morganrandall.com/dp-test。任何指导都会非常感激,因为我不太了解javascript,无法在我自己的
上真正解决这个问题答案 0 :(得分:1)
失败就在这里:
str += imgO[i].src+"
";
行尾自动为分号';'在这种情况下。如果您需要更多空行,请尝试“\ n \ n \ n”。