iframe父子页面操作方法大全

为了方便理解,我们先梳理一下结构。
    1.声明的函数方法绑定在window对象上。
    2.dom元素在window.document下(包含iframe元素)。
    window-----document------dom ( iframe ---- window ... )
    |
    function

知道这两点就方便多了,本质是获取相应页面的window与document对象

iframe获取父页面window

window.parent

父页面获取iframe window

//js
document.getElementById('iframeBack').contentWindow;
window.frames['iframeID'].contentWindow;  
window[iframeID].contentWindow;

//jq
没找到可通过 $("#iframeID")[0].contentWindow
//获取document
$("#iframeID").contents()
iframe操作父页面

    调用函数

parent.functionName();

    调用元素

 // js
window.parent.document.getElementById("id");
 // jq
$("#aa",window.parent.document);

父页面操作iframe

    调用函数

//jq
$("#iframeID")[0].contentWindow.functionName();
//js
document.getElementById('iframeID').contentWindow.functionName();
window[iframeID].contentWindow.functionName();

    调用元素

//jq
$("#iframeID").contents().find("#btn");
$("#ID",window[iframeID].contentDocument)

//js
document.getElementById('iframeID').contentWindow.document.getElementById("id")
document.getElementById('iframeID').contentDocument.getElementById("id")
window[iframeID].contentDocument.getElementById("id")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值