adiv=document.getElementById('adiv'); bdiv=document.getElementById('bdiv'); cdiv=document.getElementById('cdiv'); // ..... // ... // . // 略 zdiv=document.getElementById('zdiv'); //只对一个元素进行应用 bdiv.Onmousemove= function (activeelement) { //xxxx }
是不是只能对每个 div 元素重复地写 onmousemove 函数?
有优雅的批量写法吗?
1 Rhilip 2021-01-15 17:49:52 +08:00 这为啥不上个循环?或者如果 id 命名有规律,直接使用 querySelectorAll 来选择 doucument.querySelectorAll("xxxxx").map(x => x.Onmousemove= () => {}) |
![]() | 2 musi 2021-01-15 19:09:06 +08:00 事件委托了解一下? |
![]() | 3 shenyuzhi 2021-01-15 20:41:21 +08:00 在 body 上监听,然后在 target 上 dispatch |
![]() | 4 dd112389 2021-01-16 03:58:23 +08:00 window.addEventListener('mousemove', function (e) { // adiv or bdiv or cdiv...... let target = e.target; }); |