【发布时间】:2019-02-22 21:00:03
【问题描述】:
我有以下代码,它显示了一个包含动态数据的工具提示。 它工作正常,但它向所有人显示相同的工具提示。
我用过tip._tippy.destroy(); 位没用。
<div id="template" style="display: none;">
Loading a tooltip...
</div>
上面显示工具提示的元素:
<span class="more-tags otherPostTags" data-postId="{{$post->id}}">...</span>
Js:
const template = document.querySelector('#template')
const initialText = template.textContent
const tip = tippy('.otherPostTags', {
animation: 'shift-toward',
arrow: true,
html: '#template',
onShow() {
const content = this.querySelector('.tippy-content')
if (tip.loading || content.innerHTML !== initialText) return
tip.loading = true
node = document.querySelectorAll('[data-tippy]');
let id = node[0].dataset.postid;
$.ajax({
url: '/get/post/'+id+'/tags',
type: 'GET',
success: function(res){
let preparedMarkup = '';
res.tags.map(function(item) {
preparedMarkup +=
'<span class="orange-tag" style="background-color: '+item.color+'">'+
item.name +
'</span>';
});
content.innerHTML = preparedMarkup;
tip.loading = false
},
});
},
onHidden() {
const content = this.querySelector('.tippy-content');
content.innerHTML = initialText;
},
});
当我将鼠标悬停在上面时,tooptip 显示来自数据库的标签,但它在悬停时显示相同的标签/数据,数据不同但它显示第一次悬停时出现的工具提示。
【问题讨论】:
-
为什么所有声明都使用
const?我的意思是代码中的每个变量。 -
+1,但事实证明我在 ajax 请求中发送的 ID 是唯一的最后一个,这就是为什么工具提示相同,我的错在那里。如何使用 $(this) 获取工具提示数据属性,为每个请求发送唯一 ID?
-
@Gammer 如果您希望获得准确的示例代码,您应该添加一些标记 - 因为问题并没有真正指出代码将运行的内容 - 因此只允许通用答案。
-
@martinZeitler 更新问题。
-
@martinZeitler 问题已更新
标签: javascript php jquery twitter-bootstrap tippyjs