iframe-父子-兄弟页面相互传值(jq和js两种方法)

源码需要复制到相应的文件目录

 

 

文件目录:

 

效果图:(注意url协议不能以 filter:/  开头)

 

git地址:https://gitee.com/kaiking_g/iframe.git

 

项目结构

 

 

1.父级html源码:

main.html

 

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>main</title>
<style type="text/css">
    iframe{float:left;width:48%;height:500px;margin-left:1%;border:1px solid #eee;background:#ddd;display:table-cell;}
</style>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    var gg="这是main.html变量";
    function ggMM() {
        console.log(gg);
    }
    function callIframeMethod() {
        // js
        document.getElementById("frame").contentWindow.test();
        // jq
        $("#frame")[0].contentWindow.test(); //用jquery调用需要加一个[0]
    }
    function callIframeField() {
        // 以下两种方法可以达到同样的效果
        console.log($("#frame")[0].contentWindow.ff);
        console.log(frame.window.ff);
    }
    function callIframeHtml() {
        // 以下两种方法可以达到同样的效果
        console.log($("#frame")[0].contentWindow.$("#dd").val());
        console.log(frame.window.$("#dd").val());

        var t = document.getElementById('frame').contentWindow.document.getElementById('dd');
        console.log(t);

        // var t = document.getElementById('frame').contentWindow.document.getElementById('dd');

        //console.log($("#frame")[0].contentWindow.document.getElementById("dd").value);
        //console.log($("#frame")[0].contentWindow.document.getElementById("dd").value);                
    }    
    function giveParameter() {
        $("#frame")[0].contentWindow.hellobaby="dsafdsafsdafsdafsdafsdafsadfsadfsdafsadfdsaffdsaaaaaaaaaaaaa";
    }
</script>
</head>
<body>
    <a href="#" onClick="giveParameter();">参数传递</a>
    <a href="#" onClick="callIframeMethod();">调用子iframe方法</a>
    <a href="#" onClick="callIframeField();">调用子iframe变量</a>
    <a href="#" onClick="callIframeHtml();">调用子iframe组件</a></br>    
    <iframe id="frame" name="frame" src="frame.htm" frameborder="0"></iframe>
    <iframe id="newFrame" name="newFrame" src="newFrame.htm" frameborder="0"></iframe>
</body>
</html>

 

 

 

 

 

2.子页面

frame.html

 

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>frame</title>
<style type="text/css">
    a{display: block;line-height:30px;}
</style>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">

var ff="adfdasfdsafdsafdsaf";
function test() {
    console.log($("#dd").val());
}
function callMainField() {
    console.log(parent.gg);
}
function callMainMethod() {
    parent.ggMM();
}
function callMainHtml() {
    console.log(parent.$("#frame").attr("id"));
}
function getParameter() {
    console.log(window.hellobaby);
}

function ss(){
    console.log('这是frame方法');
    console.log(ff)
}
</script>
</head>
<body>
    <h1>frame</h1>
    <a href="#" onClick="getParameter();">接受参数</a>
    <a href="#" onClick="callMainMethod();">调用父级方法,并且打印父级变量</a>
    <a href="#" onClick="callMainField();">调用主窗口变量</a>
    <a href="#" onClick="callMainHtml();">调用子iframe组件</a>    
    <input id="dd" type="text" value="1111111111"/>
</body>
</html>

 

 

 

 

 

3.子页面中嵌套的页面

newFranme.html

 

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在frame里嵌套frame</title>
<style type="text/css">
    input,a{display: block;line-height:30px;}
    iframe{
        float:left;
        width:48%;height:250px;margin-top:40px;background:#abc;border:1px solid blue; 
    }
    #newFrame2{ float:right; }
</style>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var newFrame = {
    name:'这是newFrame的变量值',
};
function callLevelFrame() {
    var ff=parent.$("#frame")[0].contentWindow.ff;
    parent.$("#frame")[0].contentWindow.ss();
    console.log('parent.$("#frame")[0].contentWindow.ff: '+ff);
}
function callLevelFrame1() {
    console.log($("#newFrame1")[0].contentWindow.iframe1);
}

function frameFn(){
    console.log('这是frame里面的方法franmeFn');
}
$(function(){
    // setTimeout(function(){
    //     // console.log(parent.$("#frame"))
    //     // console.log(parent.$("#frame")[0].contentWindow.ss())
    //     // console.log(parent.document.getElementById('frame').contentWindow.ss());
    // },500)
})
    
</script>
</head>
<body>
    <h1>newFrame</h1>
    <a href="#" onClick="callLevelFrame();">调用兄弟iframe</a>    
    <a href="#" onClick="callLevelFrame1();">调用自己的子页面iframe1变量</a>    
    <input id="nn" type="text" value="sdafsdfsa"/>

    <iframe id="newFrame1" name="newFrame1" src="newFrame1.htm" frameborder="0"></iframe>
    <iframe id="newFrame2" name="newFrame2" src="newFrame2.htm" frameborder="0"></iframe>
</body>
</html>

 

 

 

 

 

4.子页面的子页面中嵌套的页面

newFram1.html

 

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>newFrame1</title>
<style type="text/css">
    a{display: block;line-height:30px;}
</style>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var iframe1 = '我是iframe1的变量';
function callnewFramFn() {
    // js
    parent.parent.document.getElementById('newFrame').contentWindow.frameFn();
    // jq
    parent.parent.$("#newFrame")[0].contentWindow.frameFn();


}

function callnewFramParam(){
    console.log(parent.parent.document.getElementById('newFrame').contentWindow.newFrame);
}
// $(function(){
//     setTimeout(function(){
//         // console.log(parent.parent.$("#newFrame")[0])
//         // console.log(parent.$("#newFrame")[0].contentWindow.ss())
//         // console.log(parent.document.getElementById('newFrame').contentWindow.ss());
//     },500)
// })

function frame1(){
    console.log(iframe1);
    parent.$("#newFrame2")[0].contentWindow.$('#nn2').val(iframe1);
}
</script>
</head>
<body>
    <h1>newFrame1</h1>
    <a href="#" onClick="callnewFramFn();">调用newFram方法</a>    
    <a href="#" onClick="callnewFramParam();">调用newFram变量</a>    
    <input id="nn" type="text" value="这是newFrame1的input值"/>
</body>
</html>

 

 

 

 

 

 

5.子页面的子页面中嵌套的页面

newFram2.html

 

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>newFrame1</title>
<style type="text/css">
    a{display: block;line-height:30px;}
</style>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var iframe1 = '我是iframe2的变量';
function callnewFramFn1() {
    parent.$("#newFrame1")[0].contentWindow.frame1();
    console.log(parent.$('#newFrame1'));
}

    
</script>
</head>
<body>
    <h1>newFrame2</h1>
    <a href="#" onClick="callnewFramFn1();">调用newFram1方法</a>    
    <input id="nn2" type="text" value="这是newFrame1的input值"/>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kaiking_g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值