为除触发器本身之外的所有侦听器触发事件
我有几个绑定到自定义事件的链接。该事件是通过悬停这些链接中的任何一个来触发的。但是我不希望它在我实际悬停的链接上触发。
我可以想到几个可能的解决方案
我在事件的回调函数中放置了一个“if”,并以某种方式检查触发对象是否是尝试运行回调函数并相应地处理 t 的对象。可以以方便有效的方式比较对象吗?
暂时解除绑定触发事件的对象的侦听器,然后重新绑定它。这听起来不太安全,因为事件可能会在实际触发之前反弹?我不确定如何处理这些类型的事件队列。在执行下一条语句之前,jquery.trigger 实际上会影响所有侦听器吗?另一个缺点是它可能被认为是一种黑客行为而不是良好实践?您认为?
跳过整个事件绑定触发的事情,只收集数组中的每个对象,然后让悬停回调函数迭代此操作,做我想做的事情。我想这实际上有点像上述场景中幕后实际发生的事情
有解决这个问题的常见做法吗?也许是观察者模式中的某些东西?
I have a couple of links bound to a custom event. The event is triggered by hovering any of those links. However I don't want it to be triggered on the link that I actually hovered.
I can think of a couple of possible solutions
I put an "if" in the callback function for the event and somehow check if the triggering object is the object trying to run the callback function and deal with t accordingly. Can objects be compared in a convenient and effective fashion?
Temporarily unbind the listener of the object firing the event and then rebind it afterwards. This doesn't sound bulletproof since the event might be rebound before it was actually triggered? I'm not sure how those types of event queues is handled. Will the jquery.trigger actually affect all listeners before the next statement is executed? The other downside is that it might be considered a hack instead of good practice? You opinion?
Skip the whole event-bind-trigger thing and just collect every object in an array and let the hover callback function iterate through this doing whatever I want. I guess that's actually somethink like what's actually happening behind the curtains in the to above scenarios
Is there a common practice for solving this problem? Something in the observer pattern perhaps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为每个链接指定一个 ID 和一个类。当鼠标进入某个特殊链接的活动区域时,使用鼠标悬停的链接的 ID 对所有链接触发自定义事件。让您的自定义事件处理程序进行检查,以确保该 ID 与接收事件通知的链接的 ID 不匹配。
Give each of the links an ID and a class. When the mouse enters the active area of one of your special links, fire your custom event on all of them with the ID of the link over which the mouse is hovering. Have your custom event handler check to make sure the that ID doesn't match the ID of the links that receive notice of the event.