如何取消此事件监听器

时间:2018-07-21 11:09:27

标签: javascript html

我在一个div中有四个视频,在另一个div中有另一个视频,我已经完成了一个事件监听器,该监听器将应用于您所看到的所有五个视频

var videoAct = document.querySelectorAll('video'),
    mainVideo = document.getElementById('mainvideo'), // this id for the video in (mainV )
    allVideo = document.getElementById('allvideo'), // this id for the div that contain the four video
    mainV = document.getElementById('mainv'); // this id for the div that contain one video

videoAct.forEach(function(video) {
    video.addEventListener('mouseover', function() {
        this.play();
        video.muted = true;
    });
    video.addEventListener('mouseleave', function() {
        this.pause();
    });
})

所以我如何从(mainV)中的一个视频中删除此事件侦听器,以便使用其默认设置

2 个答案:

答案 0 :(得分:1)

您不必删除活动,只需不应用它...

var allVideo = document.getElementById('allvideo'), // this id for the div that contain the four video
   videoAct = allVideo.querySelectorAll('video'); // look for videos only in the #allvideo div

videoAct.forEach(function(video){
   video.addEventListener('mouseover',function(){
      this.play();
      video.muted = true;  
   });
   video.addEventListener('mouseleave',function(){
      this.pause();    
   });
})

答案 1 :(得分:0)

您可以在id循环中检查要忽略的视频的for值,这样就不会将侦听器添加到该视频中。

var videoAct = document.querySelectorAll('video'),
   mainVideo = document.getElementById('mainvideo'), // this id for the video in (mainV )
   allVideo = document.getElementById('allvideo'), // this id for the div that contain the four video
   mainV = document.getElementById('mainv'); // this id for the div that contain one video

videoAct.forEach(function(video){
  if(video.id !== 'mainvideo') {
    video.addEventListener('mouseover',function(){
       this.play();
       video.muted = true;  
    });
    video.addEventListener('mouseleave',function(){
       this.pause();    
    });
  }
})