2013-05-29 22 views
1

舉例來說,如果我有:jQuery懸停如何知道如何撤消更改?

$("tag").hover(function() { 
    $("#overlay").toggle(); 
}); 

如何jQuery的知道如何不應用的toggle改變鼠標離開時?上面列出的代碼與一樣工作,但我想知道jQuery如何知道我在回調中做了哪些更改?我的問題不是如何手動撤消更改,因爲我知道如何做到這一點,而是如何自動撤消在回調函數中設置的更改?

回答

3

有2個版本.hover功能,

.hover 2個ARGS

  1. 處理程序的mouseenter
  2. 處理程序,鼠標離開

.hover也有單ARG版本[你擁有的版本]

  1. 處理程序都的mouseenter和鼠標離開

    $('something').hover(function() { 
        console.log('I will be called when you mouseenter and mouseleave'); 
        }); 
    

從文檔,

.hover(handlerInOut(eventObject))Returns: jQuery

將單個處理程序綁定到匹配的元素,當 鼠標指針進入或離開元素時執行。

handlerInOut(eventObject)類型:Function()當鼠標指針進入或離開元素時執行的函數。

+0

啊,好的。我認爲這是以某種方式監控這些變化。我知道了。 – sircodesalot

+0

hmm隨機downvote無評論 –

3

傳遞的一個參數.hover使得懸停進出都運行回調 - http://api.jquery.com/hover/#hover2

這是內部實現切換

toggle: function(state) { 
    var bool = typeof state === "boolean"; 

    return this.each(function() { 
     if (bool ? state : isHidden(this)) { 
      jQuery(this).show(); 
     } else { 
      jQuery(this).hide(); 
     } 
    }); 
} 

如果你還沒有傳遞的任何參數.toggle,jQuery使用此內部函數檢查元素是否隱藏(從源代碼中提取):

function isHidden(elem, el) { 
    // isHidden might be called from jQuery#filter function; 
    // in that case, element will be second argument 
    elem = el || elem; 
    return jQuery.css(elem, "display") === "none" || !jQuery.contains(elem.ownerDocument, elem); 
}