javascript中的click()无法正常运行,但在控制台中可以正常运行

时间:2019-02-06 14:10:40

标签: javascript

我想在我的网站上播放来自soundcloud的音乐,但不能在Chrome中自动播放。 因此,为解决此问题,我决定在页面加载时自动单击“播放和暂停”按钮。 但是我现在有一个新问题,而click()无法正常工作:(

代码如下:

setTimeout(function() {
  console.log("a");
  document.getElementById("play").click();
}, 1000);
    <section id="audioW">
        <a href="#" id="play" onload="autoclick('#play')"><img src="https://saeedrezvanian.com/wp-content/themes/saeedrezvanian/img/play.svg" alt="control" id="control"></a>
        <a href="https://soundcloud.com/barfakmusic" target="_blank" id="audio">Barfak</a>
    </section>

“ a”出现在控制台中,但播放按钮从未单击

我希望有人可以帮助我

1 个答案:

答案 0 :(得分:0)

反问性问题:如果只需要autoplay,他们为什么会对HTMLMediaElement.play()属性和HTMLElement.click()方法设置此限制?

您需要一个USER-gesture


对于看起来像是错字的点击问题:onload => onclick

相关问题