哪些事件附加到元素? [英] Which events are attached to an element?

查看:64
本文介绍了哪些事件附加到元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何接收 dojo 附加到元素的所有事件?

How can I receive all events attached to an element with dojo?

dojo.query('#mydiv') // which events does #mydiv has?


推荐答案

要获取DOM元素上的所有事件:

To get all events on a DOM element:

// Get my div
myDiv = dojo.byId("myDiv");
// Obtain all event-related attributes
var events = dojo.filter(
    myDiv.attributes, 
    function(item) { 
        return item.name.substr(0, 2) == 'on';
    }
);
// Execute first found event, just for fun
eval(events[0].value);

如果使用dojo.query获取myDiv,请记住dojo.query返回一个数组,因此您的元素将在myDiv [0]中。

If you get myDiv using dojo.query, remember that dojo.query returns an array, so your element would be in myDiv[0].

此解决方案不适用于dojo.connect附带的事件。可能有一种方法可以从Dojo内部工作中提取此信息,但是您必须深入研究源代码以了解操作方法。

This solution does not work with events attached with dojo.connect. There probably is a way to extract this info from Dojo inner workings, but you would have to delve into the source code to understand how.

另一种选择是,您可以显式管理具有全局注册表的所有dojo.connect事件。您可以使用dojox.collections使其更容易。例如,创建一个全局注册表,其注册表项将是dom节点,值将是dojo.connect返回的句柄(这些句柄包含dom节点,事件类型和要执行的函数):

Another option is that you explicitly manage all dojo.connect events with a global registry. You could use dojox.collections to make this easier. For example, creating a global registry whose keys will be the dom nodes, and values will be the handles returned by dojo.connect (these handles contain the dom node, the type of event and the function to execute):

// On startup
dojo.require(dojox.collections.Dictionary);
eventRegistry = new dojox.collections.Dictionary();
...
// Registering an event for dom node with id=myDiv
var handle1 = dojo.connect(dojo.byId("myDiv"), "onclick", null, "clickHandler");
// Check if event container (e.g. an array) for this dom node is already created
var domNode = handle1[0];
if (!eventRegistry.containsKey(domNode))
    eventRegistry.add(domNode, new Array());
eventRegistry.item(domNode).push(handle1);
...
// Add another event later to myDiv, assume container (array) is already created
var handle2 = dojo.connect(dojo.byId("myDiv"), "onmouseover", null, "mouseHandler");
eventRegistry.item(domNode).push(handle2);
...
// Later get all events attached to myDiv, and print event names
allEvents = eventRegistry.item(domNode);
dojo.forEach(
    allEvents, 
    function(item) {
        console.log(item[1]); 
       // Item is the handler returned by dojo.connect, item[1] is the name of the event!
    }
);

您可以隐藏烦人的检查以查看事件容器是否已通过创建dojox的子类创建。具有此支票的字典。使用此路径创建一个js文件fakenmc / EventRegistry.js,并将其放在dojo,dojox等旁边:

You can hide the annoying check to see if event container is already created by creating a subclass of dojox.collections.Dictionary with this check already incorporated. Create a js file with this path fakenmc/EventRegistry.js, and put it beside dojo, dojox, etc:

dojo.provide('fakenmc.EventRegistry');
dojo.require('dojox.collections.Dictionary');
dojo.declare('fakenmc.EventRegistry', dojox.collections.Dictionary, {
    addEventToNode : function(djConnHandle) {
        domNode = djConnHandle[0];
        if (!this.containsKey(domNode))
            this.add(domNode, new Array());
        this.item(domNode).push(djConnHandle);
    }
});

使用上述类,您需要dojo.require('fakenmc.EventRegistry')而不是' dojox.collections.Dictionary',并且直接添加dojo连接句柄而无需其他检查:

Using the above class you would have to dojo.require('fakenmc.EventRegistry') instead of 'dojox.collections.Dictionary', and would simply directly add the dojo connect handle without other checks:

dojo.provide('fakenmc.EventRegistry');
eventRegistry = new fakenmc.EventRegistry();
var handle = dojo.connect(dojo.byId("myDiv"), "onclick", null, "clickHandler");
eventRegistry.addEventToNode(handle);
...
// Get all events attached to node
var allEvents = eventRegistry.item(dojo.byId("myDiv"));
...

此代码未经测试,但我想您知道。

This code is not tested, but I think you get the idea.

这篇关于哪些事件附加到元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆