jQuery UI 1.9 には新しいツールチップ ウィジェットがあり、そのAPI ドキュメントは AJAX コンテンツを表示できることを示唆していますが、それ以上の詳細はありません。同期リクエストとブロッキングリクエストを使用してそのようなことを達成できると思いますが、これは私が望んでいることではありません。
非同期 AJAX リクエストで取得したコンテンツを表示するにはどうすればよいですか?
jQuery UI 1.9 には新しいツールチップ ウィジェットがあり、そのAPI ドキュメントは AJAX コンテンツを表示できることを示唆していますが、それ以上の詳細はありません。同期リクエストとブロッキングリクエストを使用してそのようなことを達成できると思いますが、これは私が望んでいることではありません。
非同期 AJAX リクエストで取得したコンテンツを表示するにはどうすればよいですか?
これが私のブログのjqueryuitootipウィジェットのajaxの例です。お役に立てば幸いです。
$(document).tooltip({
items:'.tooltip',
tooltipClass:'preview-tip',
position: { my: "left+15 top", at: "right center" },
content:function(callback) {
$.get('preview.php', {
id:id
}, function(data) {
callback(data); //**call the callback function to return the value**
});
},
});
これは明らかに完全な解決策ではありませんが、open
イベント中に動的にデータを取得する基本的な手法を示しています。
$('#tippy').tooltip({
content: '... waiting on ajax ...',
open: function(evt, ui) {
var elem = $(this);
$.ajax('/echo/html').always(function() {
elem.tooltip('option', 'content', 'Ajax call complete');
});
}
});
フィドルを見る
ツールチップの「コンテンツ」オプションを使用してテキストをツールチップに「AJAX」するときに注意すべきことの 1 つは、テキストの取得によってツールチップの初期化に遅延が発生することです。
ツールチップが表示された DOM ノード上でマウスが素早く移動した場合、初期化が完了する前にマウスアウト イベントが発生する可能性があります。この場合、ツールチップはまだイベントをリッスンしていません。
その結果、ツールチップが表示され、マウスをノード上に戻し、再びノードの外に移動するまで閉じません。
不要なネットワーク オーバーヘッドが発生する場合がありますが、ツールヒントを構成する前に、ツールヒント テキストを取得することを検討してください。
私のアプリケーションでは、独自の jquery 拡張機能を使用して AJAX 呼び出しを行い、結果を解析し、すべてのツールチップを初期化します。明らかに、jquery や独自の拡張機能を使用できますが、その要点は次のとおりです。
画像タグをツールチップ アンカーとして使用します。取得するテキストは名前属性で識別されます。
<img class="tooltipclassname" name="tooltipIdentifier" />
すべてのツールチップを構成するには、invoke 拡張メソッドを使用します。
$(".tooltipclassname").extension("tooltip");
拡張機能のツールチップ メソッド内:
var ids = "";
var nodes = this;
// Collect all tooltip identifiers into a comma separated string
this.each(function() {
ids = ids + $(this).attr("name") + ",";
});
// Use extension method to call server
$().extension("invoke",
{
// Model and method identify a server class/method to retrieve the tip texts
"model": "ToolTips",
"method": "Get",
// Send tooltipIds parameter
"parms": [ new myParmClass("tipIds", ids ) ],
// Function to call on success. data is a JSON object that my extension builds
// from the server's response
"successFn": function(msg, data) {
$(nodes).each(function(){
// Configure each tooltip:
// - set image source
// - set image title (getstring is my extension method to pull a string from the JSON object, remember that the image's name attribute identifies the text)
// - initialise the tooltip
$(this).attr("src", "images/tooltip.png")
.prop("title", $(data).extension("getstring", $(this).attr("name")))
.tooltip();
});
},
"errorFn": function(msg, data) {
// Do stuff
}
});
// Return the jquery object
return this;
これは、jQuery UI ツールチップで jsfiddle "/echo/html/" AJAX 呼び出しを使用する例です。
HTML:
<body>
<input id="tooltip" title="tooltip here" value="place mouse here">
</body>
JavaScript:
// (1) Define HTML string to be echo'ed by dummy AJAX API
var html_data = "<b>I am a tooltip</b>";
// (2) Attach tooltip functionality to element with id == tooltip
// (3) Bind results of AJAX call to the tooltip
// (4) Specify items: "*" because only the element with id == tooltip will be matched
$( "#tooltip" ).tooltip({
content: function( response ) {
$.ajax({
url: "/echo/html/",
data: {
'html': html_data
},
type: "POST"
})
.then(function( data ) {
response( data );
});
},
items: "*"
});
jsfiddle のこの例は次のとおりです。