【问题标题】:Audio assigned to a mouse click doesn't work, as the clickable image is h ref link分配给鼠标点击的音频不起作用,因为可点击的图像是 h ref 链接
【发布时间】:2021-05-15 09:08:38
【问题描述】:

我已将音频分配给鼠标单击,但它不起作用。可点击的图片是 h ref 链接,我想这是一个原因。您能告诉我如何更正语法以使其正常工作吗?

这是 JavaScript 中的代码:

document.querySelector(".title-image").addEventListener("click", function() {

  var audio = new Audio("sounds/birds-sounds.mp3");
      audio.play();

});

HTML 代码:

<a href="?????????.html">
              <img class="title-image" src="images/??????????.jpg" alt="????????">
            </a>

【问题讨论】:

    标签: php image click href event-listener


    【解决方案1】:

    这是另一个解决方案:

    <a onClick="return playAudio();" href="?????????.html">
        <img class="title-image" src="images/??????????.jpg" alt="????????">
    </a>
    
    <script>   
    function playAudio() {
        var audio = new Audio("sounds/birds-sounds.mp3");
        audio.play();
     
        return true;
    }   
    </script>
    

    【讨论】:

      【解决方案2】:

      这里不需要“a”标签。您可以删除 并保留 img 标签。

      您可以使用 CSS 在图像上添加指针。

      <style>
          .title-image
          {
              cursor: pointer;
          }
      </style>
      

      【讨论】:

      • 感谢您的回答。我需要 ,因为图像链接到另一个 html 页面。如果我删除 ,然后单击图像,将不会重定向到 html 页面。
      • 您可以使用 javascript 添加重定向。播放音频后。
      猜你喜欢
      • 2013-03-18
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多