在做一个播放按钮时突然用到了这个技术,具体还是蛮简单的,一看就会,就是那个音频文件的src属性动态赋值的时候花了点时间。看代码吧!
function playOrPaused(obj) {
var url = "f:/lrh.mp3";
//alert(url);
var audio = document.getElementById("audio");
if (audio) {
}
else {
var board = document.getElementById("_button");
var a2 = document.createElement("audio");
a2.setAttribute("src",url);
a2.setAttribute("controls","controls");
a2.setAttribute("id","audio");
audio = board.appendChild(a2);
}
if (audio.paused) {
audio.play();
document.getElementById("btn").value = "暂停";
return;
} else {
audio.pause();
document.getElementById("btn").value = "播放";
}
}