HTML/CSS

iframe嵌入页面自适应目标页面的高度

阿里云

iframe 嵌入页面自适应目标页面的高度,要把页面放到本地,不然会报错,并且实现不了自适应高度的功能,报错:Blocked a frame with origin "null" from accessing a cross-origin frame;原因:跨页面操作涉及域的概念(origin),错误的意思是:未捕获的安全错误:阻止了一个域为 null 的 frame 页面访问另一个域为 null 的页面。代码运行时在本地直接用浏览器打开的,地址栏是 file:///的页面,只需改为 localhost 访问就行。

第一种:

在引入 iframe 的页面加上:

也想出现在这里?联系我们
创客主机
  1. function setIframeHeight(iframe) {
  2.                 if (iframe) {
  3.                     var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
  4.                     if (iframeWin.document.body) {
  5.                         iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
  6.                     }
  7.                 }
  8.             };
  9.  
  10.             window.onload = function () {
  11.                 setIframeHeight(document.getElementById('iframe'));
  12.             };

第二种:

在引入 iframe 的页面加上:

  1. $("#iframe").load(function(){
  2.                     var mainheight = $(this).contents().find("body").height()+30;
  3.                     $(this).height(mainheight);
  4.                 });

在子页面加上:

  1. $(window.parent.document).find("#iframe").load(function(){
  2.             var main = $(window.parent.document).find("#iframe");
  3.             var thisheight = $(document).height()+30;
  4.             main.height(thisheight);
  5.         });

例子:

  1. <HTML lang="sv"><head>
  2.         <meta charset="utf-8">
  3.         <title>Iframe height demo</title>
  4.         <script src="jquery.js"></script>
  5.         <style media="screen,print">
  6.         #body {
  7.             width:70em;
  8.             max-width:100%;
  9.             margin:0 auto;
  10.         }
  11.         iframe {
  12.             width:100%;
  13.             margin:0 0 1em;
  14.             border:0;
  15.         }
  16.         </style>
  17.         <script>
  18.             function setIframeHeight(iframe) {
  19.                 if (iframe) {
  20.                     var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
  21.                     if (iframeWin.document.body) {
  22.                         iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
  23.                     }
  24.                 }
  25.             };
  26.  
  27.             window.onload = function () {
  28.                 setIframeHeight(document.getElementById('iframe'));
  29.             };
  30.         </script>
  31.     </head>
  32.     <body>
  33.         <div id="body">
  34.             <h1>Iframe height demo</h1>
  35.             <h2><code>iframe</code> <strong>without</strong> height adjustment</h2>
  36.             <iframe src="child.html"></iframe>
  37.             <h2><code>iframe</code> <strong>with</strong> height adjustment</h2>
  38.             <iframe src="child.html" frameborder="0" id="iframe"></iframe>
  39.             <div id="labfooter">
  40.     </div>
  41.         </div>
  42.  
  43.     </body>
  44.     </html>

iframe 嵌入页面自适应目标页面的高度

已有 488 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!