绑定多个事件(调整大小和加载)到addEventListener (JQuery)
Binding multiple events (resize and load) to addEventListener (JQuery)
我搜索了一下,但找不到合适的答案。网上有一些解决方案,这将是有帮助的,只是事情是,我需要改变我的整个代码结构,这不是我想要的精确。
window.addEventListener('DOMContentLoaded', function(event) {
if(window.matchMedia("(min-width: 767px)").matches){
Array.prototype.slice.call(document.getElementsByClassName("tabcontent")).forEach(function(element, index) {
element.addEventListener("mouseleave", function(event) {
event.target.style.display = "none";
});
document.getElementById("categories-list-box").addEventListener("mouseleave", function(event) {
if (event.relatedTarget.className.indexOf("tabcontent") == -1) {
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
}
})
});
}
})
function addListenerMulti(el, s, fn) {
s.split().forEach(e => el.addEventListener(e, fn, false));
}
和用法:
addListenerMulti(window, 'mousemove touchmove', function(){…});
将代码放入函数中,并使用addEventListener
调用它window.addEventListener('DOMContentLoaded', myFunction);
window.addEventListener('resize', myFunction);
function myFunction(){
if(window.matchMedia("(min-width: 767px)").matches){
Array.prototype.slice.call(document.getElementsByClassName("tabcontent")).forEach(function(element, index) {
element.addEventListener("mouseleave", function(event) {
event.target.style.display = "none";
});
document.getElementById("categories-list-box").addEventListener("mouseleave", function(event) {
if (event.relatedTarget.className.indexOf("tabcontent") == -1) {
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
}
})
});
}
}
相关文章:
- 使用javascript函数在页面初始化后加载jquery
- 什么'这是加载jQuery的最佳方式
- 在WordPress站点中加载jquery.js后加载javascript代码
- 延迟加载jquery后替换$(document).ready(function)
- 如何使用onLoad加载jQuery JSON.从Zipcode On Form获取城市和州
- 对页面重新加载 Jquery 执行操作
- 在Ajax(过滤器)加载之后加载jQuery脚本
- 异步加载JQuery时Tabbed Box已损坏
- 加载Jquery时Chrome扩展出错
- 在更改事件上加载jQuery DataTable
- 手动加载JQuery而不触发内容安全策略
- 如果更改,请在ng之后重新加载Jquery
- 脚本似乎在加载jQuery之前运行-未定义Uncaught ReferenceError:$
- 在使用jquery之前动态加载jquery
- 如何重新加载jquery,以便它能为下一个进程工作
- Firefox没有't从ajax.googleapis.com加载jquery
- 我无法在自定义Wordpress主题中成功加载jQuery
- 正在从maphilight href加载Jquery选项卡
- 如何在完全加载html之后加载jquery代码段
- 在页面加载jQuery/Javascript时,按.class对表行进行排序