首页 > 解决方案 > javascript:如果外部 .php 文件具有 .. 内联样式,则隐藏 div 永远不会改变

问题描述

故事:如果 .php 被删除或类似,则隐藏 iframe。所以我尝试隐藏包含 iframe 的 div。客户 A 的网站可以 iframe 来自我的网站(外部网站)的视频。但是如果视频被删除,它应该隐藏完整的 iframe(div)。如果视频不可用,完整的 php 将被删除或重命名。

隐藏<div>if 外部文件(我想要 iframe)不可用或命名为 .php?=123456 或没有<div "id",不管.

内联样式永远不会改变。

我尝试了上面的每一个,我没有得到它的工作。我可以编辑外部 .php 文件(我的网站也是)。无论我尝试什么,我都没有得到改变内联样式的脚本。

我想要做什么,如果“某事”隐藏div。

<div id="hide-me">
  <iframe src="https://www.external-website.com/subfolder/1250.php" style="background-color: white;border: 0;height: auto;text-align:center;width: auto;max-height: 100%;" scrolling="no"></iframe>
</div>

<script>
function yourFunctionName () {
    var anyname = document.getElementById("hide-me").style.display;
    if(document.getElementById("id-in-external-php").src == 'https://www.external-website.com/subfolder/1250.php'){
        document.getElementById("hide-me").style.display="block";
    } else {
      document.getElementById("hide-me").style.display="none";
    }

}
</script>

我在这里问了一个类似的问题,但它没有给出解决方案 在此处输入链接描述

https://www.external-website.com/subfolder/1250.php的内容:

<div id="id-in-external-php">this is content for the iframe</div>

标签: javascript

解决方案


这就是我再次运行您的代码并且它工作的方式,因此您可以尝试一下:

<div id="hide-me" style="display: none;">
    <iframe style="display: none;" id="id-in-external-php" src="https://www.external-website.com/subfolder/1250.php" style="background-color: white;border: 0;height: auto;text-align:center;width: auto;max-height: 100%;" scrolling="no"></iframe>
</div>

<script>
  const yourFunctionName = () => {
    const anyname = document.getElementById("hide-me");
    const frameId = document.getElementById("id-in-external-php");
    const check = frameId.contentDocument.body.children
    const c = check[0].innerText;
    if(c.startsWith('Cannot')) {
      anyname.style.display="none";
      frameId.style.display="none";
    } else {
      anyname.style.display="block";
      frameId.style.display="block";
    }
  }
  window.addEventListener("load", yourFunctionName, false);
</script>

我没有看到你在哪里调用了这个函数,所以我在加载窗口时调用了我的


推荐阅读