由于我已经将鼠标悬停和鼠标退出事件附加到HTML中的每个元素,因此如何防止在几个HTML元素上发生相同的事件


$('body').on('mouseover mouseout', '*:not(.printToolBar)', function (e) {
    if (this === e.target) {
        (e.type === 'mouseover' ? setMenuBox(e.target) : removeMenuBox(e.target));
    }
   });

我也尝试了.on.off方法,但无法获得所需的结果。

只需从原始绑定中排除不需要的元素:

$('body').on('mouseover mouseout', '*:not(.undesiredelements)', function (e) {
    if (this === e.target) {
        $(e.target).css('border', (e.type === 'mouseover' ? '1px solid blue' : ''));
    }
});

"删除某些元素的绑定"等效于更改为其触发处理程序的选择器条件。如果在使用松散选择器(如 *)后需要更改绑定,只需取消绑定原始处理程序并重新绑定到所需的元素即可。

请注意,这里只发生一个事件处理程序绑定(即body元素)

最新更新