将多个事件绑定到侦听器(没有JQuery)?

在处理浏览器事件时,我已经开始将Safari的touchEvents集成到移动设备中。我发现addEventListeners与条件堆叠在一起。该项目不能使用JQuery。


标准事件侦听器:


/* option 1 */

window.addEventListener('mousemove', this.mouseMoveHandler, false);

window.addEventListener('touchmove', this.mouseMoveHandler, false);


/* option 2, only enables the required event */

var isTouchEnabled = window.Touch || false;

window.addEventListener(isTouchEnabled ? 'touchmove' : 'mousemove', this.mouseMoveHandler, false);

jQuery bind允许多个事件,如下所示:


$(window).bind('mousemove touchmove', function(e) {

    //do something;

});

有没有办法像JQuery示例中那样组合两个事件侦听器?例如:


window.addEventListener('mousemove touchmove', this.mouseMoveHandler, false);

任何建议或提示表示赞赏!


茅侃侃
浏览 504回答 3
3回答

慕工程0101907

一些实现预期结果的紧凑语法,POJS:   "mousemove touchmove".split(" ").forEach(function(e){      window.addEventListener(e,mouseMoveHandler,false);    });
打开App,查看更多内容
随时随地看视频慕课网APP