添加特定的CSS样式's仅当网站在iframe或lightbox中查看时

Add certain CSS style's ONLY when the site is viewed in an iframe or lightbox

本文关键字:iframe 网站 lightbox CSS 样式 添加      更新时间:2023-09-26

每当我的网站在iframe/lightbox中查看时,我想删除我的页眉/页脚。我在网上搜索了一下,找到了下面的代码:

if (window.top!=window.self)
    {
  // In a Frame or IFrame
echo "<style> 
         .sticky-wrapper{ display: none !important;} 
         .header-contact-bar {display: none !important;}
         #as.footer { display: none !important;}
      </style>";
    }
    else
    {
  // Not in a frame
    } 

CSS在灯箱中也改变了,但在我的网站上也改变了。问题是。我不明白为什么它也添加css当我不在lightbox/iframe

  1. 我做了什么错误的代码?

  2. 代码工作吗?

  3. 我必须在else声明中放一些东西吗?

  4. 是的。怎么啦?

在困惑了一段时间后找到了自己的答案。

<script>
        if( self != top ) {
   headTag = document.getElementsByTagName("head")[0].innerHTML;
   var frameCSS = headTag + '<style type="text/css">.sticky-wrapper{ display: none !important;}  .header-contact-bar {display: none !important;} #as.footer { display: none !important;}</style>';
   document.getElementsByTagName('head')[0].innerHTML = frameCSS;
}
  </script>