JavaScript 参考手册
Audio autoplay 属性
以下实例演示了如何创建 <video> 元素及设置 autoplay 属性
源代码:
点击运行 »
<p>点击按钮创建AUDIO 元素, 一个设置了 autoplay 为 true, 另外一个设置了 autoplay 为 false。</p> <button onclick="myFunction(true)"> Audio 设置 autoplay</button> <button onclick="myFunction(false)">Audio 不设置 autoplay</button> <br> <script> function myFunction(p) { var x = document.createElement("AUDIO"); x.setAttribute("id", "myVideo"); x.setAttribute("controls", "controls"); var y = document.createElement("SOURCE"); y.setAttribute("src", "/examples/horse.ogg"); y.setAttribute("type", "audio/ogg"); x.appendChild(y); var z = document.createElement("SOURCE"); z.setAttribute("src", "/examples/horse.mp3"); z.setAttribute("type", "audio/mpeg"); x.appendChild(z); // 设置 autoplay 属性: x.autoplay = p; document.body.appendChild(x); } </script>
运行结果:
点击运行 »