2011-08-07 65 views
0

以下是来自webdesign tuts+的工具提示脚本。很好的工作,但它不能禁用默认的工具提示。编辑jquery工具提示脚本以禁用默认工具提示

我缺乏相应更新此脚本的技能。我非常感谢编辑,将禁用默认工具提示!

谢谢。

<script type="text/javascript"> 
    $("a").mouseenter(function (e) { //event fired when mouse cursor enters "a" element 
     var $class_name = $(this).attr("class").slice(13); //get class attribute of "a" element after leaving 13 characters which is equal to "tooltip_link " 
     var $x = e.pageX - this.offsetLeft; //get mouse X coordinate relative to "a" element 
     var $tooltip_text = $(this).attr("title"); //get title attribute of "a" element 

     if ($tooltip_text.length > 0) { //display tooltip only if it has more than zero characters 

      $(this).append('<div class="tooltip ' + $class_name + '">' + $tooltip_text + '</div>'); //append tooltip markup, insert class name and tooltip title from the values above 

      $("a > div.tooltip.center").css("left", "" + $x - 103 + "px"); //set tooltip position from left 
      $("a > div.tooltip.left").css("left", "" + $x + "px"); //set tooltip position from left 
      $("a > div.tooltip.right").css("left", "" + $x - 208 + "px"); //set tooltip position from left 

      $("a > div.tooltip." + $class_name).fadeIn(300); //display, animate and fade in the tooltip 
     } 
    }); 

    $("a").mouseleave(function() { //event fired when mouse cursor leaves "a" element 
     var $class_name = $(this).attr("class").slice(13); //get class attribute of "a" element after leaving 13 characters which is equal to "tooltip_link " 

     //fade out the tooltip, delay for 300ms and then remove the tooltip and end the custom queue 
     $("a > div.tooltip." + $class_name).fadeOut(300).delay(300).queue(function() { 
      $(this).remove(); 
      $(this).dequeue(); 
     }); 
    }); 
</script> 

回答

0

试试这个。当工具提示显示时,它将删除title属性,因此默认的属性不会显示,则它稍后再次设置title属性。

这里有一个例子:http://jsfiddle.net/pnc3r/2/

var $tooltip_text; 

    $("a").mouseenter(function (e) { //event fired when mouse cursor enters "a" element 
      var $class_name = $(this).attr("class").slice(13); //get class attribute of "a" element after leaving 13 characters which is equal to "tooltip_link " 
      var $x = e.pageX - this.offsetLeft; //get mouse X coordinate relative to "a" element 
      $tooltip_text = $(this).attr("title"); //get title attribute of "a" element 

      if ($(this).hasClass("tooltip_link")) { 

       $(this).attr("title",""); // removes the tooltip 
      } 

      if ($tooltip_text.length > 0) { //display tooltip only if it has more than zero characters 

       $(this).append('<div class="tooltip ' + $class_name + '">' + $tooltip_text + '</div>'); //append tooltip markup, insert class name and tooltip title from the values above 

       $("a > div.tooltip.center").css("left", "" + $x - 103 + "px"); //set tooltip position from left 
       $("a > div.tooltip.left").css("left", "" + $x + "px"); //set tooltip position from left 
       $("a > div.tooltip.right").css("left", "" + $x - 208 + "px"); //set tooltip position from left 

       $("a > div.tooltip." + $class_name).fadeIn(300); //display, animate and fade in the tooltip 
      } 
     }); 

     $("a").mouseleave(function() { //event fired when mouse cursor leaves "a" element 

      $(this).attr("title",$tooltip_text); // puts the tooltip back 
      $tooltip_text = ""; 

      var $class_name = $(this).attr("class").slice(13); //get class attribute of "a" element after leaving 13 characters which is equal to "tooltip_link " 

      //fade out the tooltip, delay for 300ms and then remove the tooltip and end the custom queue 
      $("a > div.tooltip." + $class_name).fadeOut(300).delay(300).queue(function() { 
       $(this).remove(); 
       $(this).dequeue(); 
      }); 
     }); 
+0

grc,这简直太棒了。非常感谢!但是,只是一个说明。您的编辑会禁用所有默认工具提示,而不仅仅是具有“工具提示”类别的工具提示。在不使用新工具提示的情况下,如何保持默认工具提示处于活动状态?不知道它是否意味着什么,但在html中使用的实际类是“tooltip_link”。 – user705100

+0

好吧,我已经更新了代码和示例,以便在删除工具提示之前检查该类是否为“tooltip_link”。 – grc

+0

奇怪的是,默认工具提示在链接是图像的位置仍然不起作用。尽管如此,还是比原来有所改进。再次感谢。 – user705100

0

从理论上讲,这应该工作,虽然我没有测试它:

<script type="text/javascript"> 
var $tooltip_text; 

$("a.tooltip_link").mouseenter(function (e) { //event fired when mouse cursor enters "a" element 
     var $class_name = $(this).attr("class").slice(13); //get class attribute of "a" element after leaving 13 characters which is equal to "tooltip_link " 
     var $x = e.pageX - this.offsetLeft; //get mouse X coordinate relative to "a" element 
     $tooltip_text = $(this).attr("title"); //get title attribute of "a" element 


    /* 
    * My changes: 
    */ 

     $(this).attr("title", ""); // remove title text to stop default tooltip 

     // end changes (one more in the bottom function 


    if ($tooltip_text.length > 0) { //display tooltip only if it has more than zero characters 

     $(this).append('<div class="tooltip ' + $class_name + '">' + $tooltip_text + '</div>'); //append tooltip markup, insert class name and tooltip title from the values above 

     $("a > div.tooltip.center").css("left", "" + $x - 103 + "px"); //set tooltip position from left 
     $("a > div.tooltip.left").css("left", "" + $x + "px"); //set tooltip position from left 
     $("a > div.tooltip.right").css("left", "" + $x - 208 + "px"); //set tooltip position from left 

     $("a > div.tooltip." + $class_name).fadeIn(300); //display, animate and fade in the tooltip 
    } 
}); 

$("a.tooltip_link").mouseleave(function() { //event fired when mouse cursor leaves "a" element 
    var $class_name = $(this).attr("class").slice(13); //get class attribute of "a" element after leaving 13 characters which is equal to "tooltip_link " 

    //fade out the tooltip, delay for 300ms and then remove the tooltip and end the custom queue 
    $("a > div.tooltip." + $class_name).fadeOut(300).delay(300).queue(function() { 
     $(this).remove(); 
     $(this).dequeue(); 
    }); 


    /* 
    * My changes: 
    */ 
    $(this).attr("title", $tooltip_text); // return the title text to the anchor element 
    // end changes 


}); 
</script> 

我在我的评论的变化,所以你可以看到他们,也许学习......这是假设我已经做了实际的工作。

+0

我在我的代码固定的一个错误,我有 “本” 而不是 “$(本)” – BumbleShrimp

+0

谢谢,user797115。但是,现在默认的工具提示显示所有链接,而新的工具提示根本不显示。 – user705100

+0

我很抱歉,已经很晚了,而且我实在太懒惰,无法检查我的代码。无论如何,它看起来像上面的海报“Grc”你有照顾。再次抱歉! – BumbleShrimp