【问题标题】:not sure why setAttribute() isn't working for audio element不知道为什么 setAttribute() 不适用于音频元素
【发布时间】:2019-06-01 17:38:23
【问题描述】:

在我的代码中,我想设置音频元素的属性值,以便在音频文件停止播放时将段落元素中的“测试”更改为“工作”。

我知道我可以在脚本之外创建音频元素,但我需要这个来做其他事情。

“horse.mp3”文件在那里是因为我在这个页面上玩弄试图让它工作:https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_audio

<!DOCTYPE html>
<html>

<body>
  <p id="test">testing</p>
  <script>
    var audio = new Audio("horse.mp3");

    audio.setAttribute("onended", "function(){document.getElementById("
      test ").innerHTML = "
      working "}");

    audio.play();
  </script>
</body>

</html>

【问题讨论】:

  • 你的调试控制台有错误

标签: html audio setattribute


【解决方案1】:

您不需要将onended 属性包装到函数中。此外,您在属性值中使用了多个双引号。它行不通。我已经用单引号代替了双引号。

var audio = new Audio("horse.mp3");

audio.setAttribute("onended", "document.getElementById('test').innerHTML = 'working'");

audio.play();

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2014-01-29
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2022-11-30
    • 2014-09-19
    • 1970-01-01
    • 2015-03-14
    相关资源
    最近更新 更多