javascript - 如何选择多个具有相同名称的类并触发相同的功能?
问题描述
如何使此功能选择所有类“.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();
}
});
解决方案
代表
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
}
})
推荐阅读
- javascript - 如何将变量从 JavaScript 传递到 ASP
- javascript - 我怎样才能把常量放在一个函数中而不是让它们全局化
- asp.net - System.Web.HttpContext.Current.Session 在 Azure 服务器上不起作用
- xml - XSLT 2 | 对于每个选择 | 使用 doc() 时
- automapper - Autofac - ForAllOtherMembers - 未使用映射器
- python - Zip() 函数:变量是列表的列表
- bash - 使用 egrep 复制 URL
- amazon-web-services - 使用哪个 DynamoDB 调用从 dynamodb 获取 15 个项目?
- postgresql - 将栅格数据加载到 Postgres 表时出错
- java - 扩展 com.sun.jna.Structure 的 Java 类的 Spark bean 编码器