技术解析

根据“批量对象元素集合”,去应用脚本的 onmousemove 动作,如何实现?
0
2021-08-11 03:00:42
idczone
    adiv=document.getElementById('adiv');
    bdiv=document.getElementById('bdiv');
    cdiv=document.getElementById('cdiv');
    // .....
    // ...
    // .
    // 略
    zdiv=document.getElementById('zdiv');

    //只对一个元素进行应用
    bdiv.onmousemove = function (activeelement) {
        //xxxx
    }

是不是只能对每个 div 元素重复地写 onmousemove 函数?
有优雅的批量写法吗?


这为啥不上个循环?或者如果 id 命名有规律,直接使用 querySelectorAll 来选择
doucument.querySelectorAll("xxxxx").map(x => x.onmousemove = () => {})

事件委托了解一下?

在 body 上监听,然后在 target 上 dispatch

window.addEventListener('mousemove', function (e) {
// adiv or bdiv or cdiv......
let target = e.target;
});

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服