2013-12-15 83 views
0

我对JavaScript很新,我试图把自己的图片库作为我的第一个简单的项目。我预计它会顺利进行,但似乎我正在做一些微不足道的错误,我不知道在哪里。因此,我的初步计划是:我将设置一个图像元素,其中包含空的src属性,创建一个图像名称数组,添加一个函数,该函数在触发时会将数组指针增加1,以及那么只需将数组中的对应值添加到src即可。按钮onclick没有反应

虽然不起作用。你能否试着看看代码中的任何错误?我真的不知道该怎么做,只是找不到一个错误。

<html> 
<body> 
    <img src="" id="imageCanvas"> 
    <div id="counterDisplay"></div> 

    <script type="text/javascript"> 
    var images = ["increased.jpg","knight.jpg","knight-g.jpg","golden.jpg","land.jpg"]; 

    var counterDisplay = document.getElementById("counterDisplay"); 
    var imageCanvas = document.getElementById("imageCanvas"); 
    var imageCount = 0; 

      // function intended to increase the array position indicator by 1, but it always only increases it in relation to the original imageCount value (0), how to make it save the already increased value? 
    function changeCount() { 
     imageCount = imageCount+1; 
    } 

    counterDisplay.innerHTML = imageCount; 
    imageCanvas.setAttribute("src",images[imageCount]); 

    </script> 
      // The main problem: it just refuses to respond! 
    <button onclick="changeCount()">NEXT</button> 
</body> 

非常感谢你提前。

+0

你想要的是显示计数? – Misters

回答

0

我没有通过这个调试,但我的猜测是onclick正在调用,但你没有更新该计数changeCount代码。

试着把几乎所有的代码放在changeCount

function changeCount() { 
    imageCount = imageCount+1; 
    counterDisplay.innerHTML = imageCount; 
    imageCanvas.setAttribute("src",images[imageCount]); 
} 
0

该功能在单击按钮时运行。

function changeCount() { 
    imageCount = imageCount+1; 
} 

此代码在脚本第一次加载时运行。

counterDisplay.innerHTML = imageCount; 
imageCanvas.setAttribute("src",images[imageCount]); 

您希望它在点击按钮时运行。将其移到该函数中。