html限制仅有一个音/视频可播放
js
/** 多个音频仅能播放一个 */
function audiosPlay() {
const audios = document.getElementsByTagName('audio');
const videos = document.getElementsByTagName('video');
function pauseAll() {
var self = this;
[].forEach.call(audios, function (i) {
if (i !== self) i.pause();
});
[].forEach.call(videos, function (i) {
i.pause();
});
}
[].forEach.call(audios, function (i) {
i.addEventListener('play', pauseAll.bind(i));
});
}
/** 多个视频仅能播放一个 */
function videosPlay() {
const audios = document.getElementsByTagName('audio');
const videos = document.getElementsByTagName('video');
function pauseAll() {
var self = this;
[].forEach.call(videos, function (i) {
if (i !== self) i.pause();
});
[].forEach.call(audios, function (i) {
i.pause();
});
}
[].forEach.call(videos, function (i) {
i.addEventListener('play', pauseAll.bind(i));
});
}