简体   繁体   English

在 React 中切换不可控元素的可见性

[英]Toggle Visibility of an Uncontrollable Element in React

I have Hubspot chat script embedded in the index.html file of my React App using the <script/> tag for user communication.我在我的 React 应用程序的index.html文件中嵌入了 Hubspot 聊天脚本,使用<script/>标签进行用户通信。

Now, when a button is clicked I want the hubspot container to be hidden and then when another button is clicked I want it to be displayed again.现在,当单击一个按钮时,我希望隐藏 hubspot 容器,然后当单击另一个按钮时,我希望它再次显示。

I do not have control over the behaviour of the chat icon because it was added to the page via a <script/> tag.我无法控制聊天图标的行为,因为它是通过<script/>标签添加到页面的。

But after some digging I was able to get the id of the element so here is how I've tried toggling the visibility of the chat but it's not working for me.但是经过一番挖掘后,我能够获得元素的 id,所以这就是我尝试切换聊天可见性的方法,但它对我不起作用。

const toggleHubspotContainer = (turnOn) => {
        let hubSpotContainer = document.getElementById("hubspot-messages-iframe-container");
        if (turnOn) {
            hubSpotContainer.style.display = 'block';
            return;
        }
        hubSpotContainer.style.display = 'none'; //The chat icon is not hiding even after this
    }

Please note that the hubspot script was added in the index.html file like this请注意,在index.html文件中添加了 hubspot 脚本,如下所示

<script type="text/javascript" id="hs-script-loader" async defer src="//js-eu1.hs-scripts.com/adadadad.js"></script>

Is there a way to do this properly?有没有办法正确地做到这一点?

Thank you.谢谢你。

Try this, consider your script is in header试试这个,考虑你的脚本在标题中

function hideHb() {
  var head = document.getElementsByTagName('head').item(0);
  var js = document.getElementById("hs-script-loader");
  head.parentNode.removeChild(js);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM