简体   繁体   English

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

[英]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.

相关问题 Javascript在IE中不起作用,在其他所有方面都可以正常工作 - Javascript doesn't work in IE, works fine in everything else Javascript无法在WAMP上运行,但如果没有WAMP,则可以正常运行 - Javascript doesn't work on WAMP but works fine without it .click()在页面加载时不起作用,但在控制台中工作 - .click() doesn't work on page load but works in console <a href=“..”>第一次点击不起作用,但在FF中第二次点击起作用,在IE中工作正常</a> - <a href=“..”> doesn't work at first click but works at second click in FF, in IE works fine 我的 javascript 代码不起作用,但在控制台中执行时它起作用 - My javascript code doesn't work, but when it is executed in the console it works 首次点击会显示验证错误,并且无效。 第二次点击就可以了 - First Click shows validation error and doesn't work. Second click it works fine iPhone:跨度点击无法正常工作 - iPhone: span click doesn't work fine 为什么 Jquery $(#); 不工作,但 Javascript document.getElementById 工作正常吗? - Why Jquery $(#); doesn't work but Javascript document.getElementById works fine? Javascript问题,使用localhost可以正常运行网站,使用computername不能正常工作 - Javascript issue, site works fine using localhost, doesn't work using computername JavaScript函数在Opera中工作正常,但在Firefox中根本不工作。 为什么? - JavaScript function works fine in Opera but doesn't work at all in Firefox. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM