Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iframe外部的JavaScript样式主体_Javascript_Css_Iframe_Google Dfp_Skin - Fatal编程技术网

iframe外部的JavaScript样式主体

iframe外部的JavaScript样式主体,javascript,css,iframe,google-dfp,skin,Javascript,Css,Iframe,Google Dfp,Skin,如何从iFrame最里面的body标记中使用JavaScript设置最外面的body标记的样式 <html> <head> </head> <body> <div id="ad_tag" style="width: 1px; height: 1px;"> <div id="ad_container" style="border: 0pt none;"> <iframe i

如何从iFrame最里面的body标记中使用JavaScript设置最外面的body标记的样式

<html>
  <head>
  </head>
  <body>
    <div id="ad_tag" style="width: 1px; height: 1px;">
      <div id="ad_container" style="border: 0pt none;">
        <iframe id="ad_iframe" title="title" name="name" scrolling="no" marginwidth="0" marginheight="0" style="border: 0px none; vertical-align: bottom;" srcdoc="" width="1" height="1" frameborder="0">
          <html>
            <head>
            </head>
            <body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
              <script>
              parent.document.body.style.background = "url('https://news.nationalgeographic.com/content/dam/news/photos/000/676/67655.jpg') no-repeat top center fixed #ffffff";
              </script>
            </body>
          </html>
        </iframe>
      </div>
    </div>
    <div id="content">
    </div>
  </body>
</html>

parent.document.body.style.background=“url('https://news.nationalgeographic.com/content/dam/news/photos/000/676/67655.jpg“)无重复上止点固定#ffffff”;
  • 在这种情况下:
    • 不允许直接编辑属性或样式,现有属性或样式是预设的
    • 只能编辑最内层主体的子元素
    • 目标是使用谷歌DFP提供背景图像

    • 最好的方法是从帧中发送一条post消息,父级(站点)将通过侦听器执行该消息。 因此,iframe应该包含以下代码:

      <script>
           window.parent.postMessage({
              'func': 'changeBackground',
              'message': ''
          }, "*");
      </script>
      
      
      window.parent.postMessage({
      “func”:“changeBackground”,
      “消息”:”
      }, "*");
      
      您的家长(站点)应该包含一个类似以下内容的侦听器:

      <script type="text/javascript">
      if (window.addEventListener) {
          window.addEventListener("message", onMessage, false);       
      }
      else if (window.attachEvent) {
          window.attachEvent("onmessage", onMessage, false);
      }
      
      
      
      function onMessage(event) {
          var data = event.data;
          if (typeof(window[data.func]) == "function") {
              window[data.func].call(null, data.message);
          }
      }
      
      function changeBackground(message) {
      
          $('div#backgroundwrapper').css({
              'background-image': 'url("https:/example.com/example.jpg")',
          $('div#backgroundwrapper').click();
              var win = window.open( '%%CLICK_URL_ESC%%%%DEST_URL%%', '_blank');
          win.focus();
          });
      }
      </script>
      
      
      if(window.addEventListener){
      addEventListener(“message”,onMessage,false);
      }
      else if(窗口附件){
      attachEvent(“onmessage”,onmessage,false);
      }
      函数onMessage(事件){
      var数据=事件数据;
      if(typeof(window[data.func])=“function”){
      窗口[data.func].call(null,data.message);
      }
      }
      函数更改背景(消息){
      $('div#backgroundrapper').css({
      “背景图像”:“url(“https:/example.com/example.jpg”),
      $('div#backgroundrapper')。单击();
      var win=window.open(“%%单击”\u URL\u ESC%%%%%,“\u blank”);
      win.focus();
      });
      }
      

      在我的示例中,它将把图像文件作为背景放在ID为BackgroundRapper的div上。我的示例使用jQuery更改CSS并添加clicktag,但也可以在没有它的情况下进行。iframe是否有访问父文档的权限?@CertainPerformance,是的。