2013-05-06 61 views
0

我試圖隨機替換網格中的圖像。每次觸發功能時,新圖像會短暫閃爍(在Chrome中),但是舊圖像會重新出現。我知道爲什麼我有這個問題有一個非常簡單的解釋,但當然,我無法弄清楚。任何幫助都將不勝感激。永久設置新的圖像源

該問題的網站是http://www.apocwords.net/systems/randomdungeon.html。如果您點擊Randomize Me!字樣,頂部,左側,右側和底部的塊應切換到新的隨機選擇的圖像。

function createMe() { 
document.getElementById("left2").src = xLefts[Math.floor(Math.random()* xLefts.length)]; 
document.getElementById("left3").src = xLefts[Math.floor(Math.random()* xLefts.length)]; 
document.getElementById("left4").src = xLefts[Math.floor(Math.random()* xLefts.length)]; 
} 

The HTML: 
<img src="/images/templates/template_left_05.jpg" width="100" height="200" id="left2" class="template"/> 
<img src="/images/templates/template_left_01.jpg" width="100" height="200" id="left3" class="template"/> 
<img src="/images/templates/template_left_02.jpg" width="100" height="200" id="left4" class="template"/> 

我確信有一百萬種方法可以做我想做得更好,更快,更有效的事情。但是,我現在不在乎這一點。我只是想知道爲什麼我試圖做的不工作。提前致謝!

回答

0

你的問題是你的鏈接中有「href」屬性。當你點擊它時,你會爲同一頁面發出新的請求。你可以:

  1. 刪除它並在鏈接上使用this css property,使「手」指針。
  2. 使用this jQuery method
0

變化的隨機化鏈接...

<a href="javascript:createMe(); return false;">Randomize Me!</a> 
+0

也做到了,謝謝! – CodexofRome 2013-05-06 20:46:04