首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在TrustPilot小部件中定制JS/CSS

在TrustPilot小部件中定制JS/CSS
EN

Stack Overflow用户
提问于 2019-02-13 18:20:59
回答 2查看 4.5K关注 0票数 3

我正在尝试修改我的站点上的TrustPilot小部件。

TrustPilot小部件的基本结构是:

代码语言:javascript
复制
#tp-widget_wrapper .tp-widget-wrapper
    #wrapper-top .wrapper-top
        #tp-widget-reviews-wrapper .tp-widget-reviews-wrapper hidden-element
            #tp-widget-reviews .tp-widget-reviews
                .tp-widget-review
                    .tp-widget-stars
                    .date

我正在尝试隐藏分区.date

到目前为止,我得到的结论是:

代码语言:javascript
复制
var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0];
var date_tags = trustpilot_frame.document.getElementsByClassName("date");
date_tags.style.display = "none";

然而,我得到了错误:

代码语言:javascript
复制
Uncaught TypeError: Cannot read property 'getElementsByClassName' of undefined

var trustpilot_frame正在查找iframe,但我无法访问其中的任何内容。

编辑

代码语言:javascript
复制
var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
console.log(trustpilot_frame);
var date_tags = trustpilot_frame.getElementsByClassName("date");
console.log(date_tags);
date_tags.style.display = "none";

控制台:

编辑2

代码语言:javascript
复制
var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
var style_tag = trustpilot_frame.createElement("style");
style_tag.textContent = ".date{display:none;}";
trustpilot_frame.getElementsByTagName("head")[0].appendChild(style_tag);
console.log(trustpilot_frame);

控制台:

编辑3

代码语言:javascript
复制
var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
trustpilot_frame.onload = setTimeout(hide_dates, 10000);
function hide_dates(){

    // method 1
    var style_tag = trustpilot_frame.createElement("style");
    style_tag.textContent = ".date{display:none;}";
    trustpilot_frame.getElementsByTagName("head")[0].appendChild(style_tag);
    console.log(trustpilot_frame);

    // method 2
    var date_tags = trustpilot_frame.getElementsByClassName("date");
    console.log(date_tags);
    date_tags.style.display = "none";
}

控制台显示与用于dom的EDIT2和用于date_tags的第一个编辑相同

https://codepen.io/phallihan/pen/OdwgGd

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-13 18:25:33

要获得iframe的文档,您需要使用document.getElementById('iframe').contentDocument,然后您应该能够使用getElementsByClassName

https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentDocument

更新后的代码将如下所示:

代码语言:javascript
复制
var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0];
var date_tags = trustpilot_frame.contentDocument.getElementsByClassName("date");
date_tags.style.display = "none";

更新

尝试执行以下操作,等待iframe加载。

代码语言:javascript
复制
var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0];
trustpilot_frame.onload = function() {
    var date_tags = trustpilot_frame.contentDocument.getElementsByClassName("date");
    date_tags.style.display = "none";
}
票数 4
EN

Stack Overflow用户

发布于 2019-02-25 19:23:53

我不想这样做,但我是一个低级的开发人员,我的老板坚持要我这么做。我无法通过JS/CSS做到这一点,所以最终不得不用我自己的滑块替换代码片段,并将评论保存为图像。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54667750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档