2015-03-19 50 views
0

jQuery UI API繼工具提示教程我試圖創建自定義html內容的工具提示,但沒有顯示出來。控制檯顯示我的腳本沒有錯誤,只有一些來自jQuery-UI(但沒有特別的)。jQuery的自定義內容工具提示什麼都沒有

任何人都可以告訴我我做錯了什麼嗎?

$(document).ready(function() { 
 
    $(function() { 
 
    $(document).tooltip({ 
 
     items: "custom-tooltip", 
 
     content: function() { 
 
     var element = $(this); 
 
     if (element.is("custom-tooltip")) { 
 
      return "<a>THIS<br/>IS<br/><A><br/>TOOLTIP!"; 
 
     } 
 
     } 
 
    }); 
 
    }); 
 
});
<!doctype html> 
 
<html lang="en"> 
 

 
<head> 
 

 
    <meta charset="UTF-8"> 
 
    <title>CUSTOM TOOLTIPS</title> 
 

 
    <link rel="stylesheet" href="jquery-ui.css"> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
    <script src="jquery-1.10.2.js"></script> 
 
    <script src="jquery-ui.js"></script> 
 

 
    <script src="tooltips.js"></script> 
 

 
</head> 
 

 
<body> 
 

 
    <h3><a href="#" custom-tooltip="">HOVER ME</a></h3> 
 

 
</body> 
 

 
</html>

回答

1

.is功能需要的選擇器。如果它存在匹配的屬性,圍繞着它在方括號[]

if (element.is("[custom-tooltip]")) { 

同樣與items選項:

 items: "[custom-tooltip]", 

你的代碼的其餘部分看起來不錯。請注意,由於items已將小部件限制爲相同的標籤,因此您可能根本不需要if檢查。

的有效選擇更多信息可以在文檔頁面上找到:http://api.jquery.com/category/selectors/

+0

嗯,這就是我想要它的情況下,使用簡單的東西時,如果我想在多個元素上使用多個工具提示,或者有更簡單的方法,那麼IF是否適用?就像我對5個不同元素有5個不同的工具提示一樣,我會爲每個元素做出判斷,如果是句子 – 2015-03-19 11:28:06

+0

傳統的方式是使用「title」。但是,如果你動態地加載內容,你擁有的是一個很好的簡單的方法。 [jqueryui頁面上的一個例子](https://jqueryui.com/tooltip/#custom-content)也一樣 – blgt 2015-03-19 11:32:47

1

看到這個JSFiddle。您在工具提示設置中犯了一些錯誤。 工具提示項設置應該是這樣的:items: "[custom-tooltip]" ,爲什麼你在呼喚記錄功能齊全這樣

$(document).ready(function() { -- document ready event 
    $(function() { -- same document ready 
    $(document).tooltip(...); 
    }); 
}); 

jQuery代碼會是這樣:

$(function() { 
    $(document).tooltip({ 
     items: "[custom-tooltip]", 
     content: function() { 
     var element = $(this); 
     if (element.is("[custom-tooltip]")) { 
      return "<a>THIS<br/>IS<br/><A><br/>TOOLTIP!"; 
     } 
     } 
    }); 
});