【问题标题】:using ajax with this jQuery plugin (Tooltipster)在这个 jQuery 插件中使用 ajax (Tooltipster)
【发布时间】:2015-02-20 22:30:07
【问题描述】:

我是一个老(大约 2008 年)Flash 人,更多的是设计师而不是程序员。好几年没编程了。

我正在尝试在我正在构建的网站上实现 Tooltipster(都是 html/jQuery),只有一种用法,但我需要能够按需加载内容,这样我才不会' t 有一个巨大的初始负载......一个页面上会有几十个;即一个工具提示,您可以单击其中的链接,并包含图像和文本(以及任何其他 html),并且在您移出内容区域之前不会消失。

在Tooltipster主页上,有关于用ajax加载的信息,但我不太明白。

简单来说,如何将以下代码转换为 ajax-load 实例?如前所述,一个页面上可能有很多这样的内容。例如,我认为 demo-interact 需要是一个 html 页面,所有其他实例也是如此,但我在这里不合群:

<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
  </tr>
  <tr>
    <td>      <a href="#"><span id="demo-interact" 
                                title="Try clicking 
                                    &lt;a href='http://google.com/' target='_blank'&gt;
                                        this link
                                    &lt;/a&gt;

                                    &lt;strong&gt;
                                        This text is in bold case!
                                    &lt;/strong&gt;

                                    &lt;img src='spiderman.png'/&gt;
                            ">This</span></a> will hover until you move away from the tooltip itself, i.e. it's clickable</td>
    <td>&nbsp;</td>
  </tr>
</table>

在项目的主页上,它显示了如何使用 ajax 加载 html 文件,但正如我所说,它有点超出我的能力。我假设包含标签需要一个 ID,并且该 ID 将被传递给函数而不是 example.php(如下),,或类似的东西?

$('.tooltip').tooltipster({
    content: 'Loading...',
    functionBefore: function(origin, continueTooltip) {

        // we'll make this function asynchronous and allow the tooltip to go ahead and show the loading notification while fetching our data
        continueTooltip();

        // next, we want to check if our data has already been cached
        if (origin.data('ajax') !== 'cached') {
            $.ajax({
                type: 'POST',
                url: 'example.php',
                success: function(data) {
                    // update our tooltip content with our returned data and cache it
                    origin.tooltipster('content', data).data('ajax', 'cached');
                }
            });
        }
    }
});

一切都来自这里:http://iamceege.github.io/tooltipster/#demos

任何帮助将不胜感激:)

肖恩

【问题讨论】:

  • 您是否安装了响应example.php 的网络服务器?
  • 我在 Internic.ca 有服务器空间,允许使用 php,但现在我正在使用 localhost,并且不打算使用任何 php。而且我在本地使用 WampServer64,所以我这里也有 php。但同样,在这个阶段,我不打算使用 jquery/ajax/xml 以外的任何东西。
  • AJAX 是一个与服务器异步通信的概念。这意味着,当您可以通过 HTTP 与服务器交互时,该站点不需要重新加载。根据浏览器的不同,从本地文件系统加载example.php 可能不起作用。

标签: jquery ajax tooltipster


【解决方案1】:

我知道这个问题很老,但我找到了一个可行的例子,因为我在这里遇到了同样的问题。

尝试使用此示例。它的工作原理并不是因为 XHS 限制,也不是因为这种情况。

$('#test').tooltipster({
  content: 'Loading...',
  contentAsHTML: true,
  /*optional - I used it in my case*/
  functionBefore: function(origin, continueTooltip) {

    continueTooltip();

    if (origin.data('ajax') !== 'cached') {
      $.ajax({
        type: 'POST',
        url: 'http://evolution.uni-giessen.de:8080/example/example.php',
        data: {
          somevar: "some content"
        },
        success: function(data) {
          origin.tooltipster('update', data).data('ajax ', 'cached');
        }
      });
    }
  }

});
<link href="http://evolution.uni-giessen.de:8080/example/tooltipster.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://evolution.uni-giessen.de:8080/example/jquery.tooltipster.js"></script>


<a id="test" title="a title">ein Test</a>

如果我的发帖风格有什么优化,欢迎评论。

【讨论】:

    【解决方案2】:

    如果我理解正确,您的问题是工具提示没有显示在 ajax 加载的内容中,对吗?如果是这样,您需要在上述 ajax 回调中初始化 tooltipster。

    例如,您只需要使用$('.tooltip').tooltipster(); 即可完成所有操作,但如果您正在进行 ajax 调用,则执行以下操作:

    $.ajax({
        url: '/url/goes/here',
        ...
    }).done(function() {
        // this is the callback, you can initialize tooltipster here
        $('.tooltip').tooltipster();
    
        // and now it should work with the ajax loaded stuff as well
    });
    

    【讨论】:

    • 我还没到那一步。假设我有 tooltip1.html、tooltip2.html 等,我只想将它们加载到 tooltipster 插件(在上表中的某处),当有人实际将鼠标悬停在它上面时......知道会有 tooltip(x)。 html 页面(其中很多)。你的代码对我来说有点意义,但它超出了我的理解水平。让我在 Dreamweaver 中四处逛逛,也许我可以让它发挥作用并提出更多有说服力的问题,哈哈。肖恩
    【解决方案3】:

    我不知道 tooltipster,但 Asko 的回答看起来似乎是合理的。但另一件事:$('.tooltip') 是一个 jQuery 对象,代表所有具有 CSS 类 tooltip 的 HTML 元素,但我在您的 HTML 中看不到任何此类元素。也许你打算做$('#demo-interact') - 这将代表&lt;span id="demo-interact"&gt; 元素。 ($ 接受 CSS 选择器作为参数。)

    【讨论】:

    • 我在您的 HTML 或 javascript 中没有看到任何看起来可能会告诉工具提示器哪些元素应该具有 tooltip 类的内容。但如果您使用的是 Firebug,您可以通过在 CSS 选项卡的 Selectors 下输入 .tooltip 进行检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    相关资源
    最近更新 更多