将多个事件绑定到侦听器(没有 JQuery)?
在处理浏览器事件时,我开始将 Safari 的 touchEvents 合并到移动设备中。我发现 addEventListener
与条件语句堆积在一起。 这个项目不能使用 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);
如有任何建议或提示,我们将不胜感激!
While working with browser events, I've started incorporating Safari's touchEvents for mobile devices. I find that addEventListener
s are stacking up with conditionals. This project can't use JQuery.
A standard event listener:
/* 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's bind
allows multiple events, like so:
$(window).bind('mousemove touchmove', function(e) {
//do something;
});
Is there a way to combine the two event listeners as in the JQuery example? ex:
window.addEventListener('mousemove touchmove', this.mouseMoveHandler, false);
Any suggestions or tips are appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
一些可以实现所需结果的紧凑语法,POJS:
Some compact syntax that achieves the desired result, POJS:
在 POJS 中,您一次添加一个侦听器。为同一元素上的两个不同事件添加相同的侦听器并不常见。您可以编写自己的小函数来完成这项工作,例如:
希望它能展示这个概念。
编辑 2016-02-25
Dalgard 的评论让我重新审视了这一点。我想现在更常见的是在一个元素上为多个事件添加相同的侦听器,以涵盖正在使用的各种接口类型,并且 Isaac 的答案提供了一种很好的使用内置方法来减少代码的方法(尽管代码本身较少) ,不一定是奖金)。使用 ECMAScript 2015 箭头函数扩展给出:
类似的策略可以将相同的侦听器添加到多个元素,但这样做的需要可能是事件委托的指示器。
In POJS, you add one listener at a time. It is not common to add the same listener for two different events on the same element. You could write your own small function to do the job, e.g.:
Hopefully it shows the concept.
Edit 2016-02-25
Dalgard's comment caused me to revisit this. I guess adding the same listener for multiple events on the one element is more common now to cover the various interface types in use, and Isaac's answer offers a good use of built–in methods to reduce the code (though less code is, of itself, not necessarily a bonus). Extended with ECMAScript 2015 arrow functions gives:
A similar strategy could add the same listener to multiple elements, but the need to do that might be an indicator for event delegation.
清理 Isaac 的答案:
编辑
ES6 辅助函数:
Cleaning up Isaac's answer:
EDIT
ES6 helper function:
为我;该代码工作正常,并且是使用相同(内联)函数处理多个事件的最短代码。
For me; this code works fine and is the shortest code to handle multiple events with same (inline) functions.
ES2015:
ES2015:
我为您提供了一个更简单的解决方案:
我已经对此进行了测试,它效果很好,从好的方面来说,它像这里的其他示例一样紧凑且简单。
I have a simpler solution for you:
I've tested this an it works great, on the plus side it's compact and uncomplicated like the other examples here.
您还可以使用原型将自定义函数绑定到所有元素
然后使用它
You can also use prototypes to bind your custom function to all elements
Then use it
一种方法:
One way how to do it:
AddEventListener 采用一个表示 事件类型。因此,您需要编写一个自定义函数来迭代多个事件。
这是在 jQuery 中通过使用 .split(" ") 进行处理,然后迭代列表以设置每个
类型
的事件监听器。AddEventListener take a simple string that represents event.type. So You need to write a custom function to iterate over multiple events.
This is being handled in jQuery by using .split(" ") and then iterating over the list to set the eventListeners for each
types
.下面是一个小函数,可将此功能注入到
HTMLElement
类中,使其像任何其他元素方法一样可访问:此代码注入方法
addEventListeners(events, handler, ...args) 到
HTMLElement
基类中,因此每个 HTML 元素都会获取它。例如,要让元素在发生事件后打印到控制台,请执行以下操作:
Here's a little function that injects this functionality into the
HTMLElement
class to make it accessible like any other element method:This code injects the method
addEventListeners(events, handler, ...args)
into the baseHTMLElement
class, so every HTML element will get it.To e.g. let an element print into the console once what happened to it, do like the following: