为除触发器本身之外的所有侦听器触发事件

发布于 2024-10-06 06:35:36 字数 476 浏览 2 评论 0原文

我有几个绑定到自定义事件的链接。该事件是通过悬停这些链接中的任何一个来触发的。但是我不希望它在我实际悬停的链接上触发。

我可以想到几个可能的解决方案

  1. 我在事件的回调函数中放置了一个“if”,并以某种方式检查触发对象是否是尝试运行回调函数并相应地处理 t 的对象。可以以方便有效的方式比较对象吗?

  2. 暂时解除绑定触发事件的对象的侦听器,然后重新绑定它。这听起来不太安全,因为事件可能会在实际触发之前反弹?我不确定如何处理这些类型的事件队列。在执行下一条语句之前,jquery.trigger 实际上会影响所有侦听器吗?另一个缺点是它可能被认为是一种黑客行为而不是良好实践?您认为?

  3. 跳过整个事件绑定触发的事情,只收集数组中的每个对象,然后让悬停回调函数迭代此操作,做我想做的事情。我想这实际上有点像上述场景中幕后实际发生的事情

有解决这个问题的常见做法吗?也许是观察者模式中的某些东西?

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

  1. 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?

  2. 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?

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2024-10-13 06:35:36

为每个链接指定一个 ID 和一个类。当鼠标进入某个特殊链接的活动区域时,使用鼠标悬停的链接的 ID 对所有链接触发自定义事件。让您的自定义事件处理程序进行检查,以确保该 ID 与接收事件通知的链接的 ID 不匹配。

<a class="fancy-hover" id="1" href="foo">Example Link 1</a>
<a class="fancy-hover" id="2" href="bar">Example Link 2</a>
<a class="fancy-hover" id="3" href="qux">Example Link 3</a>

<script type="text/javascript">
    // custom event
    $("a.fancy-hover").bind('mouseOverOneOfUs',function(event, whatMouseHoversOver){

        // Prevent item mouse is over from responding.
        if (whatMouseHoversOver != this.id) {
            // do something
        }
        return false; // Stop propagation up the DOM tree. Remove to allow propagation.
    });

    // When mouse enters the active area of a link with class "fancy-hover",
    //     tell all links in the class which member of the class has the mouse.
    $("a.fancy-hover").mouseenter(function() {

        // this.id is ID of current element,
        //     and we pass the value as an array to our custom event.
        $("a.fancy-hover").trigger('mouseOverOneOfUs', [this.id]);
    });
</script>

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.

<a class="fancy-hover" id="1" href="foo">Example Link 1</a>
<a class="fancy-hover" id="2" href="bar">Example Link 2</a>
<a class="fancy-hover" id="3" href="qux">Example Link 3</a>

<script type="text/javascript">
    // custom event
    $("a.fancy-hover").bind('mouseOverOneOfUs',function(event, whatMouseHoversOver){

        // Prevent item mouse is over from responding.
        if (whatMouseHoversOver != this.id) {
            // do something
        }
        return false; // Stop propagation up the DOM tree. Remove to allow propagation.
    });

    // When mouse enters the active area of a link with class "fancy-hover",
    //     tell all links in the class which member of the class has the mouse.
    $("a.fancy-hover").mouseenter(function() {

        // this.id is ID of current element,
        //     and we pass the value as an array to our custom event.
        $("a.fancy-hover").trigger('mouseOverOneOfUs', [this.id]);
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文