多年以后,又用到了窗口操作iframe的内容

本文介绍了一种通过JavaScript实现父窗体与子窗体(iframe)间交互的方法,重点讲解了如何利用不同浏览器特性的差异来获取子窗体的document和window对象,从而实现DOM操作及函数调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

main window html

<div id="abbreviative">
        <iframe src="tree-min.html" width="100%" height="100%" name="MyIFrame" id="MyIFrame" frameborder='no' scrolling="no" left="0" top="0"></iframe>
    </div>
    <script type="text/javascript">
        ///alert('中断...');
    </script>
    <script type="text/javascript" src="../../jquery/bundle/jquery-1.7.2.js"></script>
    <script type="text/javascript" src="scripts/test.js"></script>
    <div id="sample1"></div>


javascript

/** * 新增加的功能:初始化参数,参数包括iframe的document和window,对于html,可以使用document操作,函数方法,可以使用window操作

 */
var ifDoc = null;
var ifWin = null;
init = function(){
    console.log('进来 了?');
    if (document.all){//IE
        ifDoc = document.frames["MyIFrame"].document;
        ifWin = document.getElementById("MyIFrame").window;
    }else{//Firefox
        ifWin = document.getElementById('MyIFrame').contentWindow;
    ifDoc = document.getElementById('MyIFrame').contentDocument || document.getElementById('MyIFrame').contentWindow.document;
    }

}


$(function(){
    init();

    //alert(nodeid+', '+upd);
    //ifDoc.getElementById("sample1").style.background="red";;
    ifWin.t.collapseNode(nodeid, upd);
})


tree-min.html

<script type="text/javascript">
        testOne = function(){
            alert('hello,World!!!');
        }

var t = null;

testFn = function(){
    alert('调用子窗口成功!');
}

var parw = window.parent;

$(function(){
    parw.test();
    var items = $(parw.document).find('.toggle');
    items = $(parw.document.body).find('a');
    $(items).bind('click',function(){
        console.warn(this);
    });
    
})
    </script>

<body>
    <div id="sample1"></div>
    <div id="move-div"></div>
</body>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值