在Dojo Javascript库中,我了解如何使用dojo.connect或dojo.publish将我的事件处理程序连接到事件。该功能很好。在Dojo或Javascript中,如何让我的事件处理程序在其他事件处理程序之前触发?
但我想要做一件其他的东西。
我希望我的事件处理程序先火,在任何其他已定义的事件处理。我想这可以被称为“事件处理程序插入”或什么的。
有没有办法做,在道场或普通的JavaScript方便和优雅的方式?
在Dojo Javascript库中,我了解如何使用dojo.connect或dojo.publish将我的事件处理程序连接到事件。该功能很好。在Dojo或Javascript中,如何让我的事件处理程序在其他事件处理程序之前触发?
但我想要做一件其他的东西。
我希望我的事件处理程序先火,在任何其他已定义的事件处理。我想这可以被称为“事件处理程序插入”或什么的。
有没有办法做,在道场或普通的JavaScript方便和优雅的方式?
当事件被触发时,它们连接到它们的动作(onload,onclick)。那么如何在触发其他操作之前触发事件?
例如,你会怎么一个onclick
之前触发一个事件,如果你不知道它什么时候被触发?
这绝对是你不能做的事,也许你想注册处理程序到onload
事件,这个事件通常在所有其他事件之前触发。
我有一个(可能)相关的问题,以及使用的setTimeout设置标志清盘。
javascript blur event -- is there any way to detect which element now has focus? 丑陋,但它的工作原理。
尝试修改您的事件是这样的:
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
}
据我所知,一旦事件处理程序附加到DOM节点,处理程序执行的顺序是依赖于实现。