无论添加事件的顺序如何,您如何强制首先运行javascript事件? [英] How do you force your javascript event to run first, regardless of the order in which the events were added?

查看:114
本文介绍了无论添加事件的顺序如何,您如何强制首先运行javascript事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有javascript,人们在他们的页面中包含。在我的javascript中,我有一个jQuery版本(1.8为了方便参考),它被划分为自己的命名空间,并通过全局变量引用(但不是$或jQuery的两个默认变量之一) 。这允许用户在他们的页面中使用jQuery,并且不会干扰我在函数内部执行的操作。

I have javascript that people are including in their page. In my javascript I have a version of jQuery (1.8 for sake of easy reference) that is sectioned off into its own namespace, and referenced via a global variable (but not one of the two default vars of "$" or "jQuery"). This allows users to have jQuery in their page and have it not interfere with the stuff I'm doing internally in my functions.

所以我们有一个页面已经有了jQuery (1.4),并且一切正常,除了用户和我的代码都在监听元素上的click事件,并且他们是第一个,所以在他们做的几个返回false的事件上,jQuery停止传播和我的事件永远不会被触发我需要先举办活动。用户希望我的onClick功能仍能正常工作。

So we have one page that has jQuery already (1.4), and everything works fine, except that the user and my code are both listening to "click" events on elements, and theirs is going first, so on the few events they do that return false, jQuery stops propagation and my event never gets triggered. I need my event to go first. The user is expecting my onClick functionality to still work.

现在我知道jQuery通过_data()对象在内部保留自己的事件顺序,并且通过它可以取消绑定现有事件,绑定我的事件,然后重新绑定现有事件,但这仅适用于通过该jQuery实例绑定的对象。我宁愿不盲目地寻找jQuery对象,希望冲突是由用户自己的jQuery版本引入的。毕竟当用户不通过jQuery绑定事件时会发生什么?试图操纵页面中现有的jQuery对象不是一个好的解决方案。

Now I know that jQuery keeps its own order of events internally through the _data() object, and through this it is possible to unbind existing events, bind my event, then rebind the existing events, but that only applies to objects bound through that instance of jQuery. I'd rather not just blindly look for the jQuery object in hopes that the conflict was introduced by a user's own version of jQuery. After all what happens when a user binds the event not through jQuery? Trying to manipulate the existing jQuery object in the page isn't a good solution.

我知道,根据浏览器的不同,他们使用addEventListener / removeEventListener或attachEvent / detachEvent 。如果我只能获得已添加事件的列表,我可以按照我想要的顺序重新绑定它们,但我无法找到方法。通过chrome检查来查看DOM我没有看到任何地方的onclick绑定(不在对象上,也不在窗口或文档上)。

I know that, depending on browser, they are using addEventListener/removeEventListener or attachEvent/detachEvent. If only I could get a listing of the already added events, I could rebind them in the order I wanted, but I can't find out how. Looking through the DOM via chrome inspect I don't see onclick bound anywhere (not on the object, not on window or document either).

我有最好的时间试图找出jQuery绑定其收听的确切位置。为了能够控制自己事件的顺序,jQuery必须全面地听某个地方,然后开启它自己的功能吗?如果我能弄明白在哪,我可以深入了解如何确保我的活动始终是第一位的。或者也许有一些我在谷歌上找不到的JavaScript API。

I'm having the darndest time trying to figure out just exactly where jQuery binds its listening. To be able to control the order of its own events, jQuery must blanketly listen somewhere and then fire off its own functions right? If I could figure out where that's done I might get some insight into how to ensure my event is always first. Or maybe there's some javascript API I haven't been able to find on google.

有什么建议吗?

推荐答案

正如Bergi和Chris Heald在评论中所说,事实证明,没有办法从DOM中获取现有事件,也没有方法可以先插入事件。它们按照设计插入的顺序被触发,并被设计隐藏。正如一些海报所提到的,您可以访问通过jQuery的数据使用jQuery的相同实例添加的那些,但就是这样。

As Bergi and Chris Heald said in the comments, it turns out there's no way to get at the existing events from the DOM, and no method to insert events "first". They are fired in the order they were inserted by design, and hidden by design. As a few posters mentioned you have access to the ones added through the same instance of jQuery that you're using via jQuery's data, but that's it.

还有一个您可以在代码运行之前绑定的事件之前运行的情况,如果他们使用onclickHTML属性。在这种情况下,你可以编写一个包装函数,正如下面一篇相当过分的评论中所指出的那样。虽然这对我提出的原始问题的实例没有帮助,但现在非常很少有事件以这种方式绑定(大多数人和框架现在使用addEvent或attachEventListener),它是在一个场景中,你可以解决先跑的问题,而且由于很多人现在都在寻找答案,我想我会确保答案是完整的。

There is one other case where you can run before an event that was bound before your code ran, and that's if they used the "onclick" HTML attribute. In that case you can write a wrapper function, as nothingisnecessary pointed out in a rather over-the-top toned comment below. While this wouldn't help in the instance of the original question I asked, and it's now very rare for events to be bound this way (most people and frameworks use addEvent or attachEventListener underneath now), it is one scenario in which you can solve the issue of "running first", and since a lot of people visit this question looking for answers now, I thought I'd make sure the answer is complete.

这篇关于无论添加事件的顺序如何,您如何强制首先运行javascript事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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