簡體   English   中英

Javascript更改img src不起作用

[英]Javascript change img src not working

我有一個沒有指定源的簡單img,因此可以將其與javascript一起使用。 但是由於某種原因,它不起作用。

我想根據單擊的圖像更改ID為“ recipes”的img。 我要測試的唯一圖像是帶有src“ images / BTNsmoki.jpg”的段落之后的第一個圖像,但是它不起作用,因此我無法將它們彼此應用。

 function showitemrecipes(b) { if (b == "items/BTNsmoki.jpg") { var ele = document.getElementById("element"); ele.style.display = "none"; document.getElementById('recipes').innerHTML = '<font color="#FFCC00">Smoki</font>'; document.getElementById("itemrecept").src = b; } else document.getElementById('recipes').innerHTML = 'KUR'; } 
 <html> <body> <div style="clear:both;"></div> <div align="center" style="background: #000000" class="cat"> <h1 align="center"><font color="#6F249E">FORUM</font></h1> <h2 align="center"><font color="#D4A819">РЕЦЕПТИ</font></h2> <a href="#" onclick="showitemrecipes(ranec);"> <img src="items/BTNranec1.jpg" width="40px" height="40px" border="1" /> </a> <a href="#" onclick="showitemrecipes(lanec);"> <img src="items/BTNlanec.jpg" width="40px" height="40px" border="1" /> </a> <a href="#" onclick="showitemrecipes(vesnik);"> <img src="items/BTNkniga.jpg" width="40px" height="40px" border="1" /> </a> <a href="#" onclick="showitemrecipes(stap);"> <img src="items/BTNstap.jpg" width="40px" height="40px" border="1" /> </a> <p> <a href="#" onclick='showitemrecipes("items/BTNsmoki.jpg");'> <img src="items/BTNsmoki.jpg" width="40px" height="40px" border="1" /> </a> <a href="#" onclick="showitemrecipes(sokce);"> <img src="items/BTNsokce.jpg" width="40px" height="40px" border="1" /> </a> <a href="#" onclick="showitemrecipes(bluzon);"> <img src="items/BTNjakna3.jpg" width="40px" height="40px" border="1" /> </a> <a href="#" onclick="showitemrecipes(kapce);"> <img src="items/BTNkapce.jpg" width="40px" height="40px" border="1" /> </a> <p> <a href="#" onclick="showitemrecipes(papuchi);"> <img src="items/BTNpapuchi.jpg" width="40px" height="40px" border="1" /> </a> <a href="#" onclick="showitemrecipes(rakavica);"> <img src="items/BTNrakavica.jpg" width="40px" height="40px" border="1" /> </a> <a href="#" onclick="showitemrecipes(nitnarica);"> <img src="items/BTNnitnarica.jpg" width="40px" height="40px" border="1" /> </a> </div> <div align="center" style="background: #000000" id="recipes"> <font id="element" color="#FFCC00">Рецептите ќе се покажат овде</font> <p> <img id="itemrecept" /> </div> </body> </html> 

誰能告訴我為什么?

document.getElementById('recipes').innerHTML = ...正在刪除id="itemrecept"的元素

所以document.getElementById("itemrecept")慘敗

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM