[英]click() in javascript doesn't work but in console it works fine
I want to play a music from soundcloud in my website and it doesn't autoplay in Chrome. 我想在我的网站上播放来自soundcloud的音乐,但不能在Chrome中自动播放。 so to resolve this problem i decided to auto click on a play and pause button when page load.
因此,为解决此问题,我决定在页面加载时自动单击“播放和暂停”按钮。 but i have a new problem now and the click() is not working :(
但我现在有一个新问题,而click()无法正常工作:(
here's the code: 这是代码:
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" showed up in console fine but the play button never clicked “ a”出现在控制台中,但播放按钮从未单击
I hope somebody can help me 我希望有人可以帮助我
Retoric question: Why would they set up this limitation on autoplay
attribute and HTMLMediaElement.play()
method if all that were needed was an HTMLElement.click()
? 反驳性的问题:如果只需要
HTMLElement.click()
他们为什么会在autoplay
属性和HTMLMediaElement.play()
方法上设置此限制?
You need an USER-gesture . 您需要一个USER-手势 。
To your click issue that looks like a typo: onload
=> onclick
对于看起来像错字的点击问题:
onload
=> onclick
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.