我有一個動態表,加載了ajax。當我將鼠標懸停在行上時,我想要顯示工具提示,但我希望工具提示出現在某個單元格(類別爲.name
)上方,而不是整行上方。帶有手動觸發和選擇器選項的引導工具提示
此外,使用標題功能,我需要能夠獲得最近的行ID並返回一個自定義模板。
這裏是我的代碼:
<table class="table" id="myTable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
<th>Statistics</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td >1</td>
<td class="name">Name #1</td>
<td>United States of America</td>
<td>100%</td>
</tr>
<tr id="2">
<td >2</td>
<td class="name">Name #2</td>
<td>United States of America</td>
<td>50%</td>
</tr>
</tbody>
</table>
初始化:
$('#myTable').tooltip({
container: 'body',
html: true,
selector: 'td.name',
trigger: 'manual',
title: function() {
// here will be custom template
var id = $(this).parent().atrr('id');
return id;
}
});
嘗試一:Demo in jsFiddle
$('#myTable')
.on('mouseenter focusin', 'tbody > tr', function() {
$(this).find('td.name').tooltip('show');
})
.on('mouseleave focusout', 'tbody > tr', function() {
$(this).find('td.name').tooltip('hide');
});
嘗試二:Demo in jsFiddle
var tip;
$('#myTable')
.on('mouseenter focusin', 'tbody > tr', function() {
tip = $(this).find('.offer-name');
tip.tooltip(hereAllTooltipOptions);
tip.tooltip('show');
})
.on('mouseleave focusout', 'tbody > tr', function() {
tip.tooltip('hide');
});
但我不知道在很大這樣一個解決方案的性能。所以,問題是如何做到這一點,做得更好?
謝謝!!這正是我需要的:)。此外,剛剛發現我可以使用'$ cell.data('bs.tooltip')'檢查工具提示是否已經初始化。 – covfefe
「選擇器」上的Bootstrap文檔非常含糊。我完全忽略了動態工具提示可以簡單地使用tooltip()函數和選項本身即時生成的可能性。謝謝! – Angad
感謝這段代碼,我也可以用它來作爲popovers。還有一個問題:我打算在未取得任何進展的情況下使用此代碼,因爲看起來您正在自由發表。 –