javascript - 使用 JavaScript (jQuery) 插入时无法显示 TrustPilot HTML

标签 javascript jquery trustpilot

如果我直接在 HTML 页面上添加 TrustPilot html 代码,它可以正常工作,但我需要使用 jQuery 插入它。插入时我看到了 HTML 代码,但它没有显示。

  $(window).on('load', function () {
    var el = $('#some-element');

    el.html( trustPilotHtml() );

    function trustPilotHtml() {
      var str = "<div " +
        "class='trustpilot-widget' " +
        "data-locale='en-GB' " +
        "data-template-id='123456' "+
        "data-businessunit-id='123456' " +
        "data-style-height='500px' " +
        "data-style-width='100%' " +
        "data-theme='light' " +
        "data-stars='4,5' " +
        "data-schema-type='Organization'>" +
        "<a " +
        "href='https://some-url.com' target='_blank'>Trustpilot</a> " +
      "</div>";
      return $(str);
    }
});

让元素正确显示的唯一方法是直接插入到 HTML 中而不使用 JavaScript 吗?

最佳答案

不,这不是唯一的方法。

您应该在 HTML 的 HEAD 部分(或附近)内有一个引导脚本。 该脚本负责初始化 HTML 中的所有小部件(Trustpilot 行话中的 TrustBox)。

当然,如果您动态注入(inject) HTML,则这不起作用,因此也可以调用 window.Trustpilot.loadFromElement(trustbox);如果需要的话,你自己。

这里trustbox是一个 HTMLElement,您可以通过使用 document.getElementById("some-element") 来获取或类似的。

引用:https://support.trustpilot.com/hc/articles/115011421468

关于javascript - 使用 JavaScript (jQuery) 插入时无法显示 TrustPilot HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49997539/

相关文章:

javascript - 在 TrustPilot 小部件中自定义 JS/CSS

javascript - 如何在网站中导入来自各个电子邮件服务提供商的电子邮件联系人?

javascript - 如何在nodejs中创建一个有延迟的while循环

javascript - 使用 Javascript 创建动态 ID

javascript - Angular js : ng-model default value of input is not set in model

javascript - Trustpilot cross-origin error with carousel integration

c# - JQUERY 按钮返回索引元素

javascript - 使用 await 检查是否调用了 js 函数?

jquery - 使用 qTip2 检测溢出