簡體   English   中英

用純 Javascript 切換 HTML5 視頻靜音

[英]Toggle HTML5 video mute with pure Javascript

我正在嘗試使用自定義按鈕在不同視頻上切換靜音/取消靜音,而不使用 jQuery。 這是我的代碼:

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

我可以取消靜音每個視頻,但我無法再次靜音。 我在這里做錯了什么?

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