简体   繁体   English

在Dojo或Javascript中,如何使我的事件处理程序在其他事件处理程序之前启动?

[英]In Dojo or Javascript how do I make my event handler fire before other event handlers?

In the Dojo Javascript library, I understand how to use dojo.connect or dojo.publish to wire up my event handler to an event. 在Dojo Javascript库中,我了解如何使用dojo.connect或dojo.publish将事件处理程序连接到事件。 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? 在Dojo中有没有办法做到这一点,或者在普通Javascript中有没有方便又优雅的办法?

Try modifying your event like this: 尝试像这样修改事件:

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
}

When events are fired they are connected to their action (onload, onclick). 触发事件后,它们将与其动作 (onload,onclick)相关联。 So how would you fire an event before other actions are triggered? 那么,如何在触发其他操作之前触发事件?

For example, how would you fire an event before an onclick if you don't know when it's going to be triggered ? 例如,如果您不知道何时触发事件,该如何在onclick之前触发事件?

This is definitely something you can't do, maybe you want to register handlers to the onload event which is usually triggered before all the others. 这绝对是您无法做的事情,也许您想将处理程序注册到onload事件,该事件通常在所有其他事件之前触发。

I had a (possibly) related problem, and wound up using setTimeout to set a flag. 我遇到了一个(可能)相关的问题,并且最终使用setTimeout设置了一个标志。

javascript blur event -- is there any way to detect which element now has focus? javascript模糊事件-有什么方法可以检测到哪个元素现在具有焦点? ugly, but it works. 丑陋,但是行得通。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM