2010-08-27 33 views
0

我使用jQuery Tooltip控件,http://docs.jquery.com/Plugins/Tooltip,它工作得很好。不過,我有一項任務,當有人在自動完成控件中鍵入2個字符時顯示工具提示。我已經把它全部搞定了,但我不知道如何明確地顯示jQuery工具提示插件。顯式顯示jQuery.Tooltip插件

MyCompany.UI.Controls.AutoCompleteExtender = new function() { 
    /// <summary> 
    /// An extension of the autcomplete control. 
    /// </summary> 

    // Private Members 
    var _this = this; 

    // Public Members 
    this.charsBeforeShowingTooltip = 2; // Change this value via server-side code if 
             // clients want different values. 

    this.showTooltip = function() { 
     var item; 

     if (this.value.length === _this.charsBeforeShowingTooltip) { 
      // Explicitly show the tooltip after two characters have been entered. 
      alert('show the tooltip explicitly'); 
     } 
    } 
} 

,後來在服務器端生成的代碼,下面的JavaScript渲染頁面

$(document.ready(function() { 
    $('#someClientId').bind('keyup', MyCompany.UI.Controls.AutoCompleteExtender.showTooltip); 
}); 

現在除了我這一切的作品都不知道如何明確顯示工具提示插件。我試過以下,沒有他們的工作:

... 
     this.showTooltip = function() { 
      var item; 

      if (this.value.length === _this.charsBeforeShowingTooltip) { 
       // Explicitly show the tooltip after two characters have been entered. 
       $(this).hover(); // doesn't work 
       $(this).trigger('mouseover'); // doesn't work 
       $(this).trigger('mouseenter'); // doesn't work 
      } 
     } 
... 

我也嘗試添加CSS類show-tooltip(得到了來自谷歌),但也不能工作。

除了修改插件,有沒有辦法做到這一點開箱即用?

回答

0

那麼有幾個問題。我認爲有標題標籤的控件沒有(總是首先檢查明顯的!)。標題標籤實際上在表格行中,而不是表格單元格中的控件。好了,這樣鼠標懸停觸發,但是當我明確地將鼠標懸停在事件上時,jQuery Tooltip插件拋出了event.pageX和event.pageY未定義的錯誤。

他們未定義的原因是因爲該事件被明確解僱,因此沒有X/Y座標從鼠標傳遞到事件對象中。所以我做的是修改jQuery Tooltip插件來檢查它們是否也是未定義的。如果它們是工具提示插件中的helper.parent控件的offsetLeft和offsetTop屬性,則可以使用它們。

這裏是我的工具提示插件修改了代碼:再次

/** 
* callback for mousemove 
* updates the helper position 
* removes itself when no current element 
*/ 
function update(event) {  
    // ... code omitted for clarity 

    // if (event) { // This was the old check 
    if (event && typeof(event.pageX) !== "undefined" && typeof(event.pageY) !== "undefined") { 
     // ... more code omitted for clarity 
    } 

    // ... even more code omitted for clarity 
} 

感謝Joern Zaefferer創建這個插件,http://bassistance.de/jquery-plugins/jquery-plugin-tooltip