0

In the Dojo Javascript library, I understand how to use dojo.connect or dojo.publish to wire up my event handler to an event. That functionality works great.

But I want to do one additional thing.

I want my event handler to fire first, before any other already defined event handlers. I suppose this could be called "event handler insertion" or something.

Is there a way to do that in Dojo or a convenient and elegant way in plain Javascript?

4

4 回答 4

1

尝试像这样修改您的事件:

function alertTest(){
    alert('test');
}

function alertTest2(){
    alert('test2');
}

window.onload = alertTest2; //just setting it to simulate that an event
                            //is already set

oldOnload = window.onload; //store the old event into a temporary variable
window.onload = function() { //assign a new function to the event
    alertTest(); //whatever your "must happen first" function is
    oldOnload.apply(); //run the original event after
}
于 2009-02-10T19:32:37.790 回答
0

当事件被触发时,它们会连接到它们的操作(onload、onclick)。那么在触发其他操作之前如何触发事件呢?

例如,onclick如果您不知道何时触发事件,您将如何在事件之前触发它?

这绝对是您不能做的事情,也许您想将处理程序注册到onload通常在所有其他事件之前触发的事件。

于 2009-02-10T18:31:12.850 回答
0

我有一个(可能)相关的问题,最后使用 setTimeout 来设置一个标志。

javascript blur 事件——有没有办法检测现在哪个元素有焦点? 丑陋,但它的工作原理。

于 2009-02-10T19:05:04.003 回答
0

据我所知,一旦事件处理程序附加到 DOM 节点,处理程序的执行顺序取决于实现。

于 2009-02-11T08:22:50.887 回答