用Dojo解除事件 [英] Unbinding an event with Dojo
问题描述
我已经使用.on()来绑定Dojo中的一个keydown事件。事件发生后,我需要解除事件的束缚,没有任何效果。他们的文档说,事件返回一个具有.remove()方法的对象,但是对于我来说,我不知道如何访问或应用此方法。
I've used .on() to bind a keydown event in Dojo. After the event fires, I need to unbind the event, and nothing seems to work. Their documentation says that the event returns an object that has a .remove() method, but I cannot for the life of me figure out how to access or apply this method.
任何帮助将不胜感激。
Any help would be greatly appreciated.
谢谢!
query('#video-topics-input').on('keydown',function(e){
topicsDrop.keyDownFunc(e, e.keyCode);
});
推荐答案
dojo.on返回一个事件句柄,表示.remove函数解除收听者的绑定。然而,在你的情况下,你会使用一个链接的dojo.query来操作dojo.NodeList。
dojo.on returns an event-handle which has the said .remove function to unbind a listener. in your case however, youre using a chained dojo.query which operates on a dojo.NodeList.
这基本上意味着你必须考虑数组,你的上面的例子将返回一个包含一个条目的数组 - 因为选择器是一个ID。
This basically means that you must think in terms of arrays, your above example would return an array with one entry - since selector is an ID.
绑定eventlistener:
To bind eventlistener:
var eventHandles = query('#video-topics-input').on('keydown',function(e){
topicsDrop.keyDownFunc(e, e.keyCode);
});
并取消绑定这些:
eventHandles.forEach(function(handle) { handle.remove() });
更有效的方法是不要使用查询来查找byId,尽管
A more efficient approach would be to not use query to look up byId though
这篇关于用Dojo解除事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!