【发布时间】:2016-08-05 16:12:26
【问题描述】:
我找不到这个plugin 的解决方案,因为我无法通过 Ajax 找到任何工作演示或外部内容的文档。
基本上我有一个简单的div 带有鼠标悬停 JS 功能:
<div onmouseover="myFunct(this,'.$comment['id'].');" >Hover Me</div>
这是我的 JS 函数:
function myFunct(element, id){
$(element).tooltipster({
contentCloning: true,
interactive:true,
maxWidth:250,
contentAsHTML:true,
content: 'Loading...',
// 'instance' is basically the tooltip. More details in the "Object-oriented Tooltipster" section.
functionBefore: function(instance, helper) {
var $origin = $(helper.origin);
// we set a variable so the data is only loaded once via Ajax, not every time the tooltip opens
if ($origin.data('loaded') !== true) {
$.ajax({
type: "POST",
url: baseUrl+"/requests/load_profilecard.php",
data: 'id='+id+"&token_id="+token_id,
cache: false,
success: function(html) {
// call the 'content' method to update the content of our tooltip with the returned data
instance.content(html);
// to remember that the data has been loaded
$origin.html('loaded', true);
}
});
}
}
});
}
为什么工具提示只在鼠标第二次悬停时显示?
这是一个类似的例子。 JSFiddle
更新
感谢支持,这解决了我的问题:
<div class="tooltip" data-id="'.$comment['id'].'">Hover me!</div>
<script type="text/javascript">
$(document).ready(function() {
$('.tooltip').tooltipster({
content: 'Loading...',
functionBefore: function(instance, helper){
var $origin = $(helper.origin);
$.ajax({
type: "POST",
url: baseUrl+"/requests/load_profilecard.php",
data: 'id='+ $origin.attr('data-id')+"&token_id="+token_id,
cache: false,
success: function(html) {
// call the 'content' method to update the content of our tooltip with the returned data
instance.content(html);
}
});
},
interactive:true,
contentAsHTML:true,
maxWidth:250
});
});
</script>
反正这对Ajax动态内容不起作用,我也不知道为什么(我试图在Ajax页面上插入,没办法)
【问题讨论】:
-
删除
if后会发生什么?我想知道条件是否在第一次悬停时失败,然后在第二次悬停时通过。 -
@KennethStoddard 谢谢,已经尝试过了,但同样的问题。这是来自 Tooltipster 支持 - “您在 mouseover 事件已经触发时初始化您的工具提示,因此 Tooltipster 无法“听到”第一个事件并打开工具提示。您最好在页面加载时初始化工具提示并将您的评论 id 放入准备 ajax 调用时,您将在 functionBefore 中检索的 data-id 属性。"
标签: javascript jquery ajax tooltipster