【示例】
-
HTML
<audio id="music"> <source src="medias/001.ogg"> <source src="medias/001.mp3"> </audio> <button id="toggle" onclick="toggleSound()">播放</button>
-
CSS
body { background:url(images/bg.png) no-repeat; } #toggle { position: absolute; left: 93px; top: 396px; }
-
JS
function toggleSound() { var music = document.getElementById("music"); var toggle = document.getElementById("toggle"); if (music.paused) { music.play(); toggle.innerHTML = "暂停"; }else { music.pause(); toggle.innerHTML ="播放"; } }