TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: What is the best way to start autoplay?

1 点作者 maxraz超过 6 年前
As you know, some browsers prevent &#x27;autoplay&#x27; in &lt;audio id=&quot;sound&quot; src=&quot;track.mp3&quot; autoplay&gt;&lt;&#x2F;audio&gt;. I have 2 solutions and need to choose only one:<p>1) &lt;body onclick=&quot;document.getElementById(&#x27;sound&#x27;).play();&quot;&gt;<p>VS<p>2) document.addEventListener(&#x27;click&#x27;, musicPlay); function musicPlay() { document.getElementById(&#x27;sound&#x27;).play(); }<p>Thank you.

1 comment

mtmail超过 6 年前
Both do the same. (2) is more modern syntax as onclick DOM attributes are deprecated.<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;</a> might be better suited for specific programming language questions.
评论 #18376862 未加载