使用JavaScript更改图像SRC-未知错误

时间:2018-08-30 16:06:29

标签: javascript

我是第一次使用javascript,并且难以使某些功能正常工作。至关重要的是,我似乎无法执行以下代码行。

document.getElementByID("reportImage").src=filepath;

完整代码如下。

请注意,我已经创建了一个测试功能来尝试缩小故障原因的范围。前两个功能在那里展示了意图,但是当前未被。

调用。

测试功能的行为表明错误在此特定行内。值得注意的是,如果将警报放在函数的第一行,它将触发该警报,但是如果将该警报放在第二个函数,则不会触发该警报(建议此特定行在某种程度上有问题)。

这可能是一个简单的语法错误,但是我已经检查了很多次,看不到它可能是什么。请帮忙!

<!DOCTYPE html>
<html>

<script>

/* declare and set script variables */
var numberOfImages, imageArray, timing, containingFolder, i;
i = -1;
timing = 3;
containingFolder = "H:\\Images";
imageArray = [
    "FoxKey.jpg",
    "TeamKPI2.tif"
]; //imageArray should contain names of image files that sit within the specified folder

numberOfImages = imageArray.length;

function activateImageTimer() {
/* function iterates through selected images */ 
    if (numberOfImages === timingArray.length) {
        setInterval(nextImage(), timing*10);
    }
    else { alert("Please check contents of imageArray and timingArray. The 
number of images should correspond to the number of timings."); }
}

function nextImage() {
    i = (i+1) % numberOfImages;  //use modulus function to loop through array
    var filepath = containingFolder + "\\" + imageArray[i]; //build filepath
    document.getElementByID("reportImage").src=filepath;
}

function testFunction() {
document.getElementByID("reportImage").src="H:\\Images\\FoxKey.jpg";
alert("Function is functioning");
}
</script>

<body onload="testFunction()">
<img id="reportImage" src="H:\Images\TeamKPI2.tif">
</body>

</html>

1 个答案:

答案 0 :(得分:0)

JavaScript区分大小写

尝试一下:

更改:将document.getElementByID更改为document.getElementById

而且,<script></script> 必须放在bodyhead内,而不是html