• iframe上下传递对象方法


    iframe页面和原本的页面传值方法实例,可以用于获取父页面和子页面的dom对象的值。

    parent.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>Home Page For Iframe</title>
    </head>
    <body>
        <h1>This is for iframe</h1>
        <h2 id="ele">To Get it.</h2>
        <iframe src="./iframe.html" id="iframe-content" frameborder="1px">    
        </iframe>
    </body>
    </html>

    iframe.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>Iframe</title>
    </head>
    <body>
        <a href="./parent.html">Home Page</a>
        <br />
        <button onclick="closeiframe();">close iframe</button>
        <br />
        <button onclick="addele();">Add Element To Parent Page</button>
        <br />
        <button onclick="get_parent_ele();">Get Parent Page Ele</button>
        <script>
            function closeiframe() {
                var iframe_content = window.parent.document.getElementById("iframe-content");
                console.log(iframe_content);
                window.parent.document.body.removeChild(iframe_content);
            }
            function addele() {
                var new_title = document.createElement("h1");
                console.log(new_title);
                new_title.innerHTML = "Title For Page";
                window.parent.document.body.appendChild(new_title); 
            }
            function get_parent_ele() {
                var get_ele = window.parent.document.getElementById("ele");
                // alert(get_ele);
                alert(get_ele.innerHTML);
            }
    
        </script>
    </body>
    </html>

    在本地的Chrome浏览器无法使用,会出现安全错误。放在服务器上就行了。

  • 相关阅读:
    java基础(一)
    html脚本总结
    python编码规范以及推导式的编写
    性能测试
    IOS 单例分析
    IOS 从一个应用跳转另一个应用
    ios开发 如何在应用内获取当前周围wifi列表和强度 并实现在应用内控制wifi开关
    iOS 获取手机的型号,系统版本,软件名称,软件版本
    ios下最简单的正则,RegexKitLite
    网络编程总结(解析数据,下载文件,确认网络环境)
  • 原文地址:https://www.cnblogs.com/jaw-crusher/p/3553966.html
Copyright © 2020-2023  润新知