2015-09-06 104 views
0

我有一个简单的img没有指定的源,以便我可以使用它与JavaScript。但由于某种原因,它不起作用。Javascript更改img src不工作

带有id“食谱”的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>

谁能告诉我为什么?

+0

你应该只发布有关重现该问题的代码,避免所有的休息。 –

回答

2

document.getElementById('recipes').innerHTML = ...id="itemrecept"

删除元素使document.getElementById("itemrecept")失败草草收场

+0

谢谢你,我没有注意到,在我设置源代码之前,我正在删除它。 –