31

jQuery UI 1.9 中有一个新的 Tooltip Widget,其API 文档提示可以在其中显示 AJAX 内容,但没有任何进一步的细节。我想我可以通过同步和阻塞请求来完成类似的事情,但这不是我想要的。

如何让它显示通过异步 AJAX 请求检索到的任何内容?

4

4 回答 4

63

这是我博客中的jqueryui Tootip 小部件的 ajax 示例。希望对您有所帮助。

$(document).tooltip({
    items:'.tooltip',
    tooltipClass:'preview-tip',
    position: { my: "left+15 top", at: "right center" },
    content:function(callback) {
        $.get('preview.php', {
            id:id
        }, function(data) {
            callback(data); //**call the callback function to return the value**
        });
    },
});
于 2012-12-24T07:41:42.807 回答
19

这显然不是一个完整的解决方案,但它展示了在open事件期间动态获取数据的基本技术:

$('#tippy').tooltip({
    content: '... waiting on ajax ...',
    open: function(evt, ui) {
        var elem = $(this);
        $.ajax('/echo/html').always(function() {
            elem.tooltip('option', 'content', 'Ajax call complete');
         });
    }
});

小提琴

于 2012-11-05T03:21:29.537 回答
3

使用工具提示“内容”选项将文本“AJAX”放入工具提示时要注意的一件事是,文本检索会在工具提示初始化中引入延迟。

如果鼠标在带有工具提示的 dom 节点上快速移动,则可能会在初始化完成之前发生 mouse-out 事件,在这种情况下,工具提示尚未侦听该事件。

结果是显示工具提示并且在鼠标移回节点上并再次移出之前不会关闭。

虽然它会产生一些可能不需要的网络开销,但请考虑在配置工具提示之前检索工具提示文本。

在我的应用程序中,我使用自己的 jquery 扩展来进行 AJAX 调用、解析结果并初始化所有工具提示,显然您可以使用 jquery 和/或您自己的扩展,但它的要点是:

使用图像标签作为工具提示锚点,要检索的文本由名称属性标识:

<img class="tooltipclassname" name="tooltipIdentifier" />

使用调用扩展方法配置所有工具提示:

$(".tooltipclassname").extension("tooltip");

在扩展的 tooltip 方法内部:

    var ids = "";
    var nodes = this;

    // Collect all tooltip identifiers into a comma separated string
    this.each(function() {
        ids = ids + $(this).attr("name") + ",";
    });

    // Use extension method to call server
    $().extension("invoke", 
    {
        // Model and method identify a server class/method to retrieve the tip texts
        "model": "ToolTips", 
        "method": "Get",

        // Send tooltipIds parameter 
        "parms":  [ new myParmClass("tipIds", ids ) ],

        // Function to call on success. data is a JSON object that my extension builds
        // from the server's response
        "successFn": function(msg, data) {

            $(nodes).each(function(){

                // Configure each tooltip:
                // - set image source
                // - set image title (getstring is my extension method to pull a string from the JSON object, remember that the image's name attribute identifies the text)
                // - initialise the tooltip
                $(this).attr("src", "images/tooltip.png")
                    .prop("title", $(data).extension("getstring", $(this).attr("name"))) 
                    .tooltip();
            });
        },
        "errorFn": function(msg, data) { 
            // Do stuff
        }
    }); 

    // Return the jquery object
    return this;
于 2013-07-24T02:58:06.127 回答
3

这是一个使用带有 jQ​​uery UI 工具提示的 jsfiddle "/echo/html/" AJAX 调用的示例。

HTML:

<body>
<input id="tooltip" title="tooltip here" value="place mouse here">
</body>

JavaScript:

// (1) Define HTML string to be echo'ed by dummy AJAX API
var html_data = "<b>I am a tooltip</b>";

// (2) Attach tooltip functionality to element with id == tooltip
// (3) Bind results of AJAX call to the tooltip
// (4) Specify items: "*" because only the element with id == tooltip will be matched
$( "#tooltip" ).tooltip({
    content: function( response ) {
        $.ajax({ 
        url: "/echo/html/",
        data: {
                'html': html_data
            },
        type: "POST"
        })
          .then(function( data ) {
             response( data );
          });
    },
    items: "*"
});

这是jsfiddle上的这个例子:

于 2016-07-06T18:22:37.300 回答