我没有测试过这段代码,但这应该可以工作:
<a id="audioTag1"><audio src="1.mp3" ></audio>1</a>
var canPlay = false;
var audio = document.getElementsByTagName('audio')[0];
document.getElementById('audioTag1').addEventListener('click', function(){
playAudio(15);
});
audio.addEventListener('canplay', function() { // listen for the `canplay`event, to make sure the file is loaded.
canPlay = true; // Set a boolean to "true" once the audio has loaded.
});
function playAudio(time){
audio.currentTime = time; // jump to 15 secs into the file
audio.play(); // I'm not certain if this line is necessary.
}
确保在事件侦听器中运行 JS 代码onload
,以便在执行代码时加载音频标签。