页面上多个audio只播放一个
// ts版-vue private justPalyOne() { const audios = document.querySelectorAll("audio"); // 暂停函数 const pauseAll = (exclude: any): any => { audios.forEach((audio, index) => { if (exclude !== index) { audio.pause(); } }); }; audios.forEach((audio, index) => { audio.addEventListener("play", ()=>{pauseAll(index)}); }); }
// 普通js版 const justPalyOne = () => { const audios = document.querySelectorAll("audio"); // 暂停函数 const pauseAll = (exclude)=> { audios.forEach((audio, index) => { if (exclude !== index) { audio.pause(); } }); }; audios.forEach((audio, index) => { audio.addEventListener("play", ()=>{pauseAll(index)}); }); }