简体   繁体   English

如何选择多个具有相同名称的类并触发相同的功能?

[英]How to select multiple classes with the same name and fire the same function?

How to make this function select all class ".stop" and when we click on the element that has this class stop the video?如何让这个函数选择所有类“.stop”,当我们点击拥有这个类的元素时停止视频?

window.addEventListener("load", function(event) {
    window.addEventListener('scroll', checkScroll, false);
    window.addEventListener('resize', checkScroll, false);

    let playvideo = document.querySelector('video');

    let close = document.querySelector('.stop');


    close.onclick = () => {
        playvideo.pause();
    }
});

Delegate代表

const container = document.getElementById('videoContainer'); // or whatever you have
container.addEventListener('click',e => {
  const tgt = e.target;
  if (tgt.classList.contains('stop')) {
    tgt.closest('div').querySelector('video').pause(); // assuming the stop is in the same div
  }
})

Thank you, everyone, for the help, I used this script and it's work as I want谢谢大家的帮助,我使用了这个脚本,它按我的意愿工作

    let playvideo = document.querySelector('video');

    document.querySelectorAll('.stop').forEach(item => {
        item.addEventListener('click', event => {
            //handle click
            playvideo.pause();
        })
    })

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

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