使用(tree Cut.src =“images / treeCut.png”;)更改图像的src

时间:2014-01-20 02:52:42

标签: javascript html

我的想法是将图像从完整树更改为剪切树并返回到完整树。我的问题是当我将其更改为图像未显示的剪切树的图片时,但是当它切换回来时,完整树的图像工作正常。这是与问题有关的脚本:

var treeCut;

switch(randomEvent){
        case 1:
         //add a log
         document.getElementById("treeActive").innerHTML = "inActive1";
         treeSpawn=setTimeout(spawnTree,5000);
                 //below is the piece i'm having issues with
         treeCut = document.getElementById("treeButton");
         treeCut.src = function() {
             treeCut.src = "images/treeCut.png";
         } 
        break;

//not showing the full switch just the part that is being exicuted.

function spawnTree(){
    document.getElementById("treeActive").innerHTML = "Active";
    count = 0;
    document.getElementById("message").innerHTML = "chop " + count;
    clearInterval(treeSpawn);
        //brings the tree back to original image
    treeCut.src = "images/tree.png";
}

是否有问题,因为第二张图片需要有自己的标签?我已检查,双重检查,并三重检查图像是否在正确的位置并正确命名。

1 个答案:

答案 0 :(得分:1)

您可以直接设置src图片,例如,更改:

treeCut.src = function() {
    treeCut.src = "images/treeCut.png";
} 

treeCut.src = "images/treeCut.png";