jquery hover将变量传递给回调函数

前端之家收集整理的这篇文章主要介绍了jquery hover将变量传递给回调函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在悬停时删除链接标题属性,然后在鼠标移出时将其添加回来.我想将var hoverText传递给悬停…

这是我的代码.有任何想法吗?

  1. $(".icon a").hover(function() {
  2. $this = $(this);
  3. var hoverText = $.data(this,'title',$this.attr('title'));
  4. $(this).find("em").animate({opacity: "show",top: "-35"},"slow");
  5. $(this).find("em").text(hoverText);
  6.  
  7. $this.removeAttr('title');
  8.  
  9.  
  10. },function(hoverText) {
  11.  
  12. $(this).find("em").animate({opacity: "hide",top: "-45"},"fast");
  13. $(this).attr("title",hoverText);
  14.  
  15. });

解决方法

将“hoverText”作为全局变量.
  1. var hoverText = '';
  2. $(".icon a").hover(function() {
  3. $this = $(this);
  4. hoverText = $.data(this,function() {
  5.  
  6. $(this).find("em").animate({opacity: "hide",hoverText);
  7.  
  8. });

猜你在找的jQuery相关文章