【问题标题】:Change HTML image source in a div with javascript when click on label not working单击标签不起作用时,使用 javascript 在 div 中更改 HTML 图像源
【发布时间】:2021-01-12 07:06:54
【问题描述】:

单击标签时,我需要更改 HTML 中 div 内的图像。我认为我不需要为此使用 jquery,我只想使用 javascript。经过一番研究,它看起来很简单,所有的答案都是一样的,但我无法让它工作。我知道单击标签时的功能有效,因为标签改变了颜色,但图像没有改变,我没有收到任何语法错误。我正在使用铬。我不知道我做错了什么。

这是 HTML/CSS 代码:

<html>
<body>
  <div id="slc_on_label">
    <img src=https://ichef.bbci.co.uk/news/800/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg>
  </div>

  <style>
    #slc_on_label img {
      position:fixed;
      width:5.5%;
      left:10%;
      top:10%;
    }
  </style>

  <label class = "button" id="button" onclick="run(this)">0</label>

  <style>
  .button {background-color:Powderblue;
          font-size: 5vw;
        }
   </style>

  <script src="question.js" ></script>
</body>
</html>

这是javascript代码:

function run(button) {
  document.getElementById("slc_on_label").src="https://cdn.pixabay.com/photo/2016/12/13/05/15/puppy-1903313_1280.jpg";
  button.style = "background-color:red";
}

【问题讨论】:

  • 您需要将 id slc_on_label 放在图像上而不是放在 div 上...您要更改 div 的 src? div div 没有来源。
  • 确实如此!谢谢@Talmacel Marian Silviu

标签: javascript html css image src


【解决方案1】:

您正在尝试在&lt;div id="slc_on_label"&gt; 上设置src 属性,而不是其中的&lt;img&gt;

你可以使用document.querySelector()选择div里面的图片:

document.querySelector("#slc_on_label img").src =
    "https://cdn.pixabay.com/photo/2016/12/13/05/15/puppy-1903313_1280.jpg";

【讨论】:

    【解决方案2】:

    <html>
    
    <head>
        <style>
            #slc_on_label img {
                position: fixed;
                width: 5.5%;
                left: 10%;
                top: 10%;
            }
    
            .button {
                background-color: Powderblue;
                font-size: 5vw;
            }
        </style>
    </head>
    
    <body>
        <div id="slc_on_label">
            <img src="https://ichef.bbci.co.uk/news/800/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg">
        </div>
    
        <label class="button" id="button" onclick="run(this)">0</label>
    
        <script>
            function run(button) {
                document.querySelector("#slc_on_label img").setAttribute('src', 'https://cdn.pixabay.com/photo/2016/12/13/05/15/puppy-1903313_1280.jpg');
                button.style = "background-color:red";
            };
        </script>
    </body>
    
    </html>

    【讨论】:

    • 您编写代码的方式,您正在尝试更改 div 的 src。要解决这个问题,请使用您已经使用 document.querySelector("#slc_on_label img") 编写的相同选择器正确找到元素 img。现在选择正确的元素,您可以更改 src 值。
    【解决方案3】:
    function run(button) {
      document.querySelector('slc_on_label img').src =
        'https://cdn.pixabay.com/photo/2016/12/13/05/15/puppy-1903313_1280.jpg';
      button.style.backgroundColor = 'red';
    }
    

    也不破坏按钮样式。

    【讨论】:

      猜你喜欢
      • 2014-06-20
      • 1970-01-01
      • 2017-06-20
      • 2020-08-17
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多