• iframe 根据内容自动改变大小


    最近工作遇到一个iframe的问题 就是iframe需要根据内容高度变化 筛选了无数个搜索引擎返回的数据 得到以下解决方案

    function reinitIframe(){
        var iframe = document.getElementById("frame_content");
        try{
            var bHeight = iframe.contentWindow.document.body.scrollHeight,
                dHeight = iframe.contentWindow.document.documentElement.scrollHeight,
                height = Math.max(bHeight, dHeight);
                iframe.height =  height;
        }catch (ex){}
    
    }
    window.setInterval("reinitIframe()", 200);
    </script> 

    原理就是 不断刷新页面获得iframe内容页面的高度 赋值给iframe

    以下是整个测试页面代码

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ifram 内有树形节点实例</title>
        <script type="text/javascript">
    function reinitIframe(){
        var iframe = document.getElementById("frame_content");
        try{
            var bHeight = iframe.contentWindow.document.body.scrollHeight,
                dHeight = iframe.contentWindow.document.documentElement.scrollHeight,
                height = Math.max(bHeight, dHeight);
                iframe.height =  height;
        }catch (ex){}
    
    }
    window.setInterval("reinitIframe()", 200);
    </script> 
    </head>
    <body style="background-color: red;">
        <iframe scrolling="no" id="frame_content" style=" 100%;" onload="reinitIframe();" src="./subIframe.html" frameborder="0"></iframe>
    </body>
    </html>
    subIframe.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>iframe 内容页面</title>
        <style type="text/css">
        body{
            background: #ccc;
        }
        </style>
        <script type="text/javascript">
        function CreateNode(str)    
       {  
          //创建新div  
          var NewDiv = document.createElement("div");   
          //对div设置 id属性  
            NewDiv.id = "dd";   
          //创建div内加入的内容  
          var NewText = document.createTextNode(str);  
          //追加一个新的子结点  
            NewDiv.appendChild(NewText);  
          //返回新创建结点数据  
          return NewDiv;                                  
       }  
        function addDome() {
            var node =CreateNode('hello world');
            console.log(node);
            document.body.appendChild(node);
        }
        
        </script>
    
    </head>
    <body>
    <ul>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>hello every body 1</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
        <li>dd</li>
    </ul>
        <input type="button" value="add dome" onclick="addDome();">
        <a href="./2.html">首页</a>
    </body>
    </html>
  • 相关阅读:
    The last access date is not changed even after reading the file on Windows 7
    渗透基础——获得当前系统已安装的程序列表
    Top 10 Best Free Netflow Analyzers and Collectors for Windows
    平安科技移动开发二队技术周报(第十五期)
    Intent传递对象的几种方式
    SQLite基础学习
    2 Java基础语法(keyword,标识符,凝视,常量,进制转换,变量,数据类型,数据类型转换)
    iOS 之应用性能调优的25个建议和技巧
    Fragment事务管理源代码分析
    CMake
  • 原文地址:https://www.cnblogs.com/ElvinLong/p/4510501.html
Copyright © 2020-2023  润新知