工具提示器突然不工作

Tooltipster suddenly not working

我一直在为我正在构建的需要工具提示的网站尝试 jQuery 插件。工具提示在悬停时使用 AJAX 提供数据,并且在我为项目添加工具提示部分之前一切正常且花花公子。添加代码并保存后,我注意到工具提示和一些 jQuery 元素不再工作(xbbcode 插件也停止工作,但 bootstrap、颜色选择器和放大仍然工作正常).

我返回并删除了代码,看看我是否在那里做错了,但错误仍然存​​在!除了额外的工具提示外,我没有改变任何其他东西,所以我不明白为什么突然之间它现在不起作用了。这是使用的代码,包括我试图添加的项目工具提示(在底部定位 .item)。

$(document).ready(function() {

// Retrieve stored data
var bannerData = amplify.store("banner");
var haData = amplify.store("ha");
var petData = amplify.store("pet");

if (bannerData === true) {
    $('#banner').addClass('hidden');
    $('#banner-collapse').text("Expand [+]");
}

if (haData === true) {
    $('#ha').addClass('hidden');
    $('#collapse-ha').text("Expand Avatar [+]");
}

if (petData === true) {
    $('#pet').addClass('hidden');
    $('#collapse-pet').text("Expand Pet [+]");
}

// Section collapse
$('#banner-collapse').click(function () {
    if (bannerData === true) {
        $('#banner').slideToggle(function () {
            $(this).removeClass("hidden");
        });

        amplify.store("banner", null);
        $(this).text(function(i, text) {
            return text === "Expand [+]" ? "Collapse [-]" : "Expand [+]";
        });
    }

    else {
        $('#banner').slideToggle();
        amplify.store("banner", true);
        $(this).text(function (i, text) {
            return text === "Collapse [-]" ? "Expand [+]" : "Collapse [-]";
        });
    }
});

// Avatar Collapse
$('#collapse-ha').click(function () {
    if (haData === true) {
        $('#ha').slideToggle(function () {
            $(this).removeClass("hidden");
        });

        amplify.store("ha", null);
        $(this).text(function(i, text) {
            return text === "Expand Avatar [+]" ? "Collapse Avatar [-]" : "Expand Avatar [+]";
        });
    }

    else {
        $('#ha').slideToggle();
        amplify.store("ha", true);
        $(this).text(function (i, text) {
            return text === "Collapse Avatar [-]" ? "Expand Avatar [+]" : "Collapse Avatar [-]";
        });
    }
});

// Pet Collapse
$('#collapse-pet').click(function () {
    if (petData === true) {
        $('#pet').slideToggle(function () {
            $(this).removeClass("hidden");
        });

        amplify.store("pet", null);
        $(this).text(function(i, text) {
            return text === "Expand Pet [+]" ? "Collapse Pet [-]" : "Expand Pet [+]";
        });
    }

    else {
        $('#pet').slideToggle();
        amplify.store("pet", true);
        $(this).text(function (i, text) {
            return text === "Collapse Pet [-]" ? "Expand Pet [+]" : "Collapse Pet [-]";
        });
    }
});

//Formats date
var now = moment().format("dddd, MMMM Do, <b>h:mm A</b>");
$('#date').append(now);

// Tooltips
$('.tooltip-ha').tooltipster({
    animation: 'grow',
    delay: 200,
    trigger: 'hover',
    position: 'right',
    contentAsHTML: true,
    functionInit: function(origin, content) {
        // when the request has finished loading, we will change the tooltip's content
        $.ajax({
            type: 'GET',
            url: '/haTooltip',
            success: function(data) {
                origin.tooltipster('content', data);
            }
        });

        // this returned string will overwrite the content of the tooltip for the time being
        return 'Wait while we load new content...';

    }
});

$('.tooltip').tooltipster({
    animation: 'grow',
    delay: 200,
    trigger: 'hover',
    position: 'right',
    contentAsHTML: true,
    functionInit: function(origin, content) {
        // when the request has finished loading, we will change the tooltip's content
        $.ajax({
            type: 'GET',
            url: '/petTooltip',
            success: function(data) {
                origin.tooltipster('content', data);
            }
        });

        // this returned string will overwrite the content of the tooltip for the time being
        return 'Wait while we load new content...';

    }
});

$('.item').tooltipster({
    animation: 'grow',
    delay: 200,
    trigger: 'hover',
    position: 'right',
    contentAsHTML: true,
    functionInit: function(origin, content) {
        // when the request has finished loading, we will change the tooltip's content
        $.ajax({
            type: 'GET',
            url: '/itemTooltip',
            success: function(data) {
                origin.tooltipster('content', data);
            }
        });

        // this returned string will overwrite the content of the tooltip for the time being
        return 'Wait while we load new content...';

    }
});
});

我在这里没有看到任何可见的错误,我将所有插件脚本都放在一个文件中并进行了缩小以最大程度地减少加载时间,但是为了弄清楚为什么 xbbcode 和 tooltipster 突然不起作用,我继续了并重新下载了单独的版本。通常在这种情况下,我会怀疑 tooltipster 和 xbbcode 之间存在某种兼容性错误,但在添加项目工具提示之前它们工作得很好,当我暂时删除 xbbcode 以查看工具提示是否会再次开始工作时,结果是一样。

我没受过教育的是带有class "item"的对象对于工具提示无效

如果我知道 HTML 代码,我就能做出更好的猜测。

你有一些问题。

关于xbbcode

您似乎删除了实际初始化 XBBCODE 的 Javascript 调用。

将以下内容添加到您的 javascript 将解决问题:

   $('.bbcode').each(function(){
       var $this = $(this);
       var myText = $this.html();
       var result = XBBCODE.process({
            text: myText
        }); 
       $this.html(result.html);
   });

关于工具提示

  1. 您需要包含可以下载的 CSS 文件 here
  2. 将您的初始化代码更改为:

  $('.item').tooltipster({
        content: 'Loading...',
        updateAnimation: false,
        functionBefore: function(origin, continueTooltip) {
            continueTooltip();

            if (origin.data('ajax') !== 'cached') {

                // when the request has finished loading, we will change the tooltip's content
            $.ajax({
                type: 'POST', 
                url: 'http://dodsoftware.com/sotests/xbbcode/itemTooltip', // change to your file location
                success: function(data) {
                    origin.tooltipster('content', data);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                     console.log(textStatus, errorThrown);
                }
            });
                origin.data('ajax', 'cached');
            }
        }
    });