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

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

问题描述

如何使用 dojo 收到与元素相关的所有活动?

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

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


推荐答案

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

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(evens[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的子类创建。 collections.Dictionary与此检查已经合并。使用此路径fakenmc / EventRegistry.js创建一个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.

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

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