简体   繁体   English

用纯 Javascript 切换 HTML5 视频静音

[英]Toggle HTML5 video mute with pure Javascript

I'm trying to toggle mute/unmute on different videos with a custom button, without using jQuery.我正在尝试使用自定义按钮在不同视频上切换静音/取消静音,而不使用 jQuery。 Here's my code:这是我的代码:

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;
    }
  });
});

I am able to unmute each video, but I'm not able to mute it again.我可以取消静音每个视频,但我无法再次静音。 What am I doing wrong here?我在这里做错了什么?

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;
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM