【问题标题】:Why the Ajax content of this jQuery plugin (Tooltipster) is shown only after mouse hover 2 times?为什么这个 jQuery 插件 (Tooltipster) 的 Ajax 内容只有在鼠标悬停 2 次后才会显示?
【发布时间】: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


【解决方案1】:

我建议做一些事情:首先,将 JS 与 HTML 分开(这被认为是最佳实践),其次,在页面加载时初始化 Tooltipster,最后,删除包装函数。初始化工具提示后,您的代码将默认在悬停时触发。

将 JS 与 HTML 分离

<div class="hover-me tool-tip" data-commentId="12345">Hover Me</div>

在文档就绪时初始化 Tooltipster

$(document).ready(function() {
    $('.tooltip').tooltipster();
});

悬停触发工具提示器

$('.hover-me').tooltipster({
    // all of your code from above

    functionBefore: function(){
        var commentId = $(this).attr('data-commentId');
    }
});

注意:

Tooltipster 的默认触发器是 hover(如文档中所示),但是,您可以通过传入 trigger: hover 来显式设置它,这将使您的代码更具可读性和可维护性。

Tooltipster 支持建议(见comments)

我添加这个是因为它强化了我上面的解决方案,并为未来的开发者添加了上下文......那个,它可能会在 cmets 部分被忽略。

当鼠标悬停事件已经被触发时,您会初始化工具提示,因此 Tooltipster 无法“听到”第一个事件并打开工具提示。您最好在页面加载时初始化您的工具提示,并将您的评论 id 放在一个 data-id 属性中,您将在准备 ajax 调用时在 functionBefore 中检索该属性。

【讨论】:

  • 您只缺少一件事,data-id 属性...我想是这样的吧? var commentID = $(this).attr('data-id');
  • 是的,这听起来很正确,答案已更新。唯一的另一件事(我个人没有机会验证)是从functionBefore 上的instance 参数内部访问this。如果上述方法不起作用,请尝试。
  • 第三个sn-p应该更正了,见github.com/iamceege/tooltipster/issues/…
  • 你能说得具体一点吗,有什么需要更正的?谢谢
  • 应该类似于$('.tooltip').tooltipster({ functionBefore: function(instance, helper){ var $origin = $(helper.origin), commentId = $origin.attr('data-id'); } });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多