W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h3>演示如何创建 SOURCE 元素</h3> <p>点击按钮来创建两个 SOURCE 元素,并把它们添加到 AUDIO 元素。</p> <audio controls id="myAudio" autoplay> Your browser does not support the audio tag. </audio><br> <p id="demo"></p> <button onclick="myFunction()">试一下</button> <script> function myFunction() { var x = document.createElement("SOURCE"); x.setAttribute("src", "/i/horse.mp3"); x.setAttribute("type", "audio/mpeg"); document.getElementById("myAudio").appendChild(x); var y = document.createElement("SOURCE"); y.setAttribute("src", "/i/horse.ogg"); y.setAttribute("type", "audio/ogg"); document.getElementById("myAudio").appendChild(y); document.getElementById("demo").innerHTML = "音频播放器可以工作了,点击播放按钮会听到一段声音。"; } </script> </body> </html>