Javascript 更改 img src 不起作用

标签 javascript

我有一个简单的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") 惨败

关于Javascript 更改 img src 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32424324/

相关文章:

javascript - 按升序对对象数组进行排序,但将所有零放在最后

javascript - 上传大图像文件时 Ajax 响应为空

javascript - ReactJS 在 JavaScript 中使用 Web 组件

JavaScript:如何在不使用 Object.defineProperty 的情况下定义不可枚举的方法?

javascript - 这个参数是如何传递给监听器的?

javascript - ReactJS:无法在浏览器上显示html文本

javascript - Facebook API - 使用 JSSDK 登录在后端使用 php sdk

javascript - 在 Node.js 中,如果多次请求,是否需要多次加载所需的模块?

javascript - 如何使用 javascript 单击 <span>。 <span> 没有类或 id

javascript - 为 Array.prototype 添加一个 getter