Dojo.Connect Event doesn't get called - why?

2019-06-27 02:03发布

问题:

I am trying to connect an onMouseDown event to an image with dojo.connect like:

dojo.connect(dojo.byId("workpic"), "onMouseDown", workpicDown);

function workpicDown()
{
    alert("mousedown");
}

Similar code a few lines later, where I'm connecting onMouse* events to dojo.body does work completely properly.

but when I click on the image, I'm not seeing the alert window, so the event doesn't get called. Why is that?

回答1:

"onMouseDown" should be all lower case when used with DOM events as opposed to Widget events. Try:

dojo.connect(dojo.byId("workpic"), "onmousedown", workpicDown);

From the documentation:

A note about the event names: Event names now are lower case, except in special cases (e.g., some Mozilla DOM events). Dojo will add "on" to your event name if you leave it off (e.g., 'click' and 'onclick' are the same thing to dojo). This differs from Widget Events in the sense Dijit uses mixedCase event names, to avoid potential conflicts.



回答2:

Probably it's problem with execution context. Try to use fallowing:

dojo.connect(dojo.byId("workpic"), "onMouseDown",window, "workpicDown");

window.workpicDown = function()
{
    alert("mousedown");
}