2014-01-20 79 views
1

我的想法是將圖像從完整樹更改爲切割樹並返回到完整樹。我的問題是,當我將其更改爲圖像未顯示的切割樹的圖片時,但當它切換回完整樹的圖像時效果很好。這裏是與該問題有關的腳本:使用(treeCut.src =「images/treeCut.png」;)更改img的src

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"; 
} 

是否有問題,因爲第二個圖像需要自己的標記?我已經檢查,雙重檢查,並且三重檢查了圖像是否在正確的位置並且正確命名。

+0

正如你說你已經檢查了圖像被放置在正確的根與正確的名稱可能是您使用的按鈕ID不正確。你可以在函數內部使用alert來檢查函數是否被編譯或者不被編譯? – Jinandra

回答

1

你可能只是設置圖像的src直接一樣,變化:

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

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

Pefect,謝謝! –

+0

不客氣.. :)如果這解決了你的問題,你可以把它標記爲Accepted Answer .. :) –