2013-05-29 21 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); 
}