2010-12-05 100 views
0

我有一對夫婦的綁定到自定義事件的鏈接。通過懸停任何這些鏈接觸發事件。但我不希望它在我實際懸停的鏈接上觸發。觸發所有監聽事件除了觸發器本身

我能想到幾個可能的解決方案

  1. 的我把「如果」在該事件的回調函數,並以某種方式檢查觸發對象是試圖運行回調函數的對象,相應地處理。可以用方便有效的方式比較對象嗎?

  2. 暫時解除綁定觸發事件對象的監聽器,然後重新綁定之後。這聽起來並不簡單,因爲事件在實際觸發之前可能會反彈。我不知道如何處理這些類型的事件隊列。在執行下一條語句之前,jquery.trigger是否會影響所有偵聽器?另一個缺點是它可能被認爲是黑客而不是良好的做法?你的意見?

  3. 跳過整個事件綁定觸發的事情,只是收集每一個對象在數組中,並讓懸停回調函數通過這個做我想做的事情重複。我想這實際上是一個像在幕後的實際發生的事情

是否有解決此問題的常見做法?可能是觀察者模式中的某些東西?

+1

「我有一對夫婦的綁定到自定義事件的聯繫。本次活動由任何徘徊這些鏈接觸發。不過,我不希望它被我居然徘徊的鏈接觸發的事。」你真的要解釋。這沒有太大的意義。 – Jere 2010-12-05 13:27:31

回答

0

給每個鏈路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>