使用纯Javascript切换HTML5视频静音
我正在尝试使用自定义按钮在不同的视频上切换静音/取消静音,而不使用jQuery。这是我的密码:使用纯Javascript切换HTML5视频静音,javascript,button,html5-video,Javascript,Button,Html5 Video,我正在尝试使用自定义按钮在不同的视频上切换静音/取消静音,而不使用jQuery。这是我的密码: const videoContainerCollection = document.getElementsByClassName('video-container'), videoContainerArray = [...videoContainerCollection]; videoContainerArray.forEach(function(e) { const video = e.quer
const videoContainerCollection = document.getElementsByClassName('video-container'),
videoContainerArray = [...videoContainerCollection];
videoContainerArray.forEach(function(e) {
const video = e.querySelector('video'),
button = e.querySelector('button');
video.muted = true;
button.addEventListener('click', function() {
button.classList.toggle('muted');
if (video.muted = true) {
video.muted = false;
}
else if (video.muted = false) {
video.muted = true;
}
});
});
我可以取消每个视频的静音,但无法再次将其静音。我做错了什么?检查if条件,你应该比较,你分配。啊哈!谢谢!:)
const videoContainerCollection = document.getElementsByClassName('video-container'),
videoContainerArray = [...videoContainerCollection];
videoContainerArray.forEach(function(e) {
const video = e.querySelector('video'),
button = e.querySelector('button');
video.muted = true;
button.addEventListener('click', function() {
button.classList.toggle('muted');
if (video.muted === true) {
video.muted = false;
}
else if (video.muted === false) {
video.muted = true;
}
});
});