如果我直接在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吗?
答案 0 :(得分:2)
不,这不是唯一的方法。
您应该在HTML的HEAD部分(或靠近它)内部有一个引导脚本。 该脚本负责初始化HTML中的所有小部件(Trustpilot术语中的TrustBox)。
如果您正在动态注入HTML,那么原因并不起作用,因此如果您需要,也可以自己致电window.Trustpilot.loadFromElement(trustbox);
。
此处trustbox
是HTMLElement,您可以使用document.getElementById("some-element")
或类似内容。
答案 1 :(得分:1)
以下内容在产品列表页面上为我工作,该页面通过ajax返回过滤列表
var element = document.getElementsByClassName("trustpilot-widget");
for(var i=0; i<element.length; i++) {
window.Trustpilot.loadFromElement(element[i]);
}
在第一个页面加载时,所有产品评论均按预期显示,但是如果通过ajax调用更新页面(例如使用过滤器),则评论将丢失。在ajax之后运行上面的代码,重新加载评论