原文出处

iframe的调用包括以下几个方面:(调用包含html dom,js全局变量,js方法) 主页面调用iframe; iframe页面调用主页面; 主页面的包含的iframe之间相互调用;

主要知识点

1:document.getElementById("ii").contentWindow 得到iframe对象后,就可以通过contentWindow得到iframe包含页面的window对象,然后就可以正常访问页面元素了;

2:$("#ii")[0].contentWindow  如果用jquery选择器获得iframe,需要加一个【0】;

3:$("#ii")[0].contentWindow.$("#dd").val() 可以在得到iframe的window对象后接着使用jquery选择器进行页面操作;

4:$("#ii")
[0].contentWindow.hellobaby="dsafdsafsdafsdafsdafsdafsadfsadfsdafsadfdsaffdsaaaaaaaaaaaaa";

可以通过这种方式向iframe页面传递参数,在iframe页面window.hellobaby就可以获取到值,hellobaby是自定义的变量;

5:在iframe页面通过parent可以获得主页面的window,接着就可以正常访问父亲页面的元素了;

6:parent.$("#ii")[0].contentWindow.ff; 同级iframe页面之间调用,需要先得到父亲的window,然后调用同级的iframe得到window进行操作;

源码

源码包含内容,主页面(main.html)中含有两个iframe子页面(frame.html,newIframe.html)

    1. 主页面如何调用子页面中的方法;

    2. 子页面如何调用主页面中的方法;

    3. 两个子iframe之间如何如何进行交互 ;

main.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>显示图表</title>
<script src="http://www.cnblogs.com/http://www.cnblogs.com/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var gg="dsafdsafdsafdsafsdaf";
function ggMM() {
alert("2222222222222222222222222222222");
}
function callIframeMethod() {
//document.getElementById("ii").contentWindow.test();
$("#ii")[0].contentWindow.test(); //用jquery调用需要加一个[0]
}
function callIframeField() {
alert($("#ii")[0].contentWindow.ff);
}
function callIframeHtml() {
alert($("#ii")[0].contentWindow.$("#dd").val());
//alert($("#ii")[0].contentWindow.document.getElementById("dd").value);
//alert($("#ii")[0].contentWindow.document.getElementById("dd").value);
}
function giveParameter() {
$("#ii")[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="ii" src="frame.htm" width="100%" frameborder="0"></iframe>
<iframe id="new" src="newFrame.htm" width="100%" frameborder="0"></iframe>
</body>
</html>

frame.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>显示图表</title>
<script src="http://www.cnblogs.com/http://www.cnblogs.com/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript"> var ff="adfdasfdsafdsafdsaf";
function test() {
alert($("#dd").val());
}
function callMainField() {
alert(parent.gg);
}
function callMainMethod() {
parent.ggMM();
}
function callMainHtml() {
alert(parent.$("#ii").attr("id"));
}
function getParameter() {
alert(window.hellobaby);
}
</script>
</head>
<body>
<a href="#" onClick="getParameter();">接受参数</a>
<a href="#" onClick="callMainMethod();">调用子iframe方法</a>
<a href="#" onClick="callMainField();">调用主窗口变量</a>
<a href="#" onClick="callMainHtml();">调用子iframe组件</a>
<input id="dd" type="text" value="1111111111"/>
</body>
</html>

兄弟iframe页面 newIframe.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>显示图表</title>
<script src="http://www.cnblogs.com/http://www.cnblogs.com/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function callLevelFrame() {
var ff=parent.$("#ii")[0].contentWindow.ff;
alert(ff);
}
</script>
</head>
<body>
<a href="#" onClick="callLevelFrame();">调用兄弟iframe</a>
<input id="nn" type="text" value="sdafsdfsa"/>
</body>
</html>

如果是在easyui的tab的选项卡中,有个实例这么做:

//得到被选中的tab对象
var tab=parent.$("#tabs").tabs("getSelected");
//该iframe是在tab选项卡中的一个内嵌的iframe,获取该页面的id为tableId的值
tab.panel("body").find("iframe")[0].contentWindow.$("#tableId").val(id);
//获取被选中的tab的内嵌页面iframe的id为clcs的datagrid,并重新加载数据
tab.panel("body").find("iframe")[0].contentWindow.$('#clcs').datagrid('reload');

[转]内嵌页面iframe以及和其兄弟iframe的相互传值的更多相关文章

  1. 如何根据iframe内嵌页面调整iframe高宽续篇

    接着昨天的工作 如何根据iframe内嵌页面调整iframe高宽 来说,按照文章中说的第二种方法实现代码如下: 实现 A.com/detail/view 页面的iframe代码如下: <ifra ...

  2. Python3.x:selenium获取iframe内嵌页面的源码

    Python3.x:selenium获取iframe内嵌页面的源码 前言 在一些网页中经常会看到ifrmae/frame标签,iframe是嵌入式框架一般用来在已有的页面中嵌入另一个页面,当一个元素在 ...

  3. WebBrowser内嵌页面的跨域调用问题

    很早之前我写过一篇Blog:网页通过External接口与WebBrowser交互,文中的交互其实只介绍了JS调用C++的部分,而C++调用JS由于微软自己的例子太多,那篇文章就没介绍,不过我最近遇到 ...

  4. 如何根据iframe内嵌页面调整iframe高宽

    问题来自于工作的实例,我的一个域名A的页面,有个iframe,它可能内嵌了另一个域名B的页面,也可能内嵌域名C的页面,但是呢,B和C的页面大小是不一样的,特别是高是不一样的高,那么我如何设置ifram ...

  5. 弹出iframe内嵌页面元素到父页面并全屏化

    (注册博客好久了,一直没舍得添砖加瓦,主要是每次想写点东西的时候,随便搜一搜发现都比我总结的都要好,甚感尴尬,但是总是要开始的,所以这就是我的第一篇博客,也绝不会是最后一篇,废话不多说,直接入正题) ...

  6. iframe内嵌页面——跨域通讯

      <template> <div class="act-form"> <iframe :src="src" ref=" ...

  7. iframe动态改变内嵌页面高度

    test.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://w ...

  8. 一个页面中内嵌页面 iframe元素

    iframe.html: <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...

  9. ifame内嵌页面全屏完美展示

    <body style= marginwidth= marginheight= width='100%' height='100%' allowfullscreen='true' src='ht ...

随机推荐

  1. [swustoj 443] Handsome Swap

    Handsome Swap(0443) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 89 Accepted: 20 Accepte ...

  2. 【转】Mac用户必备!100多款免费实用的苹果Mac软件大搜集

    原文网址:http://www.iplaysoft.com/100-mac-freeware.html 对于 Mac 新手,尤其是刚刚从 Windows 转到 Mac OS X 的用户来说,最大的痛苦 ...

  3. Google Chrome中的高性能网络(一)

    以下内容是"The Performance of Open Source Applications" (POSA)的草稿, 也是The Architecture of Open S ...

  4. 在Eclipse中使用Github(EGit)

    安装配置EGit 1. 安装Windows版的Git,登陆Github账号,登陆成功后会自动在本地和Github配置好密钥 2. 在Eclipse中安装EGit,地址http://download.e ...

  5. [liu yanling]软件测试用例的基本要素包括哪些?

    用例编号: 测试用例的编号有一定的规则,比如系统测试用例的编号这样定义规则: PROJECT1-ST-001 ,命名规则是项目名称+测试阶段类型(系统测试阶段)+编号.定义测试用例编号,便于查找测试用 ...

  6. mysql服务启动 但端口未监听

    mysql 启动了,用 localhost 可以连接,但是用 127.0.0.1 不能连接.可能的原因是 1. mysql为了增强安全性而跳过了端口监听,查看方法: 用mysql> SHOW V ...

  7. [TVYJ1096]数字组合

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 在N个数中找出其和为M的若干个数.先读入正整数N(1<N<100)和M(1<M<100 ...

  8. c语言之sizeof总结

    一.sizeof的概念 Sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.Sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是一个表达式或括在括号内的 ...

  9. 在VirtualBox中安装了Ubuntu后,Ubuntu的屏幕分辨率非常小,操作非常不便。通过安装VirtualBox提供的“增强功能组件”,-摘自网络

    在VirtualBox中安装了Ubuntu后,Ubuntu的屏幕分辨率非常小,操作非常不便.通过安装VirtualBox提供的“增强功能组件”,可以解决这一问题,并且使用非常方便. 一.环境 | En ...

  10. Codeforces Round #341 (Div. 2) ABCDE

    http://www.cnblogs.com/wenruo/p/5176375.html A. Wet Shark and Odd and Even 题意:输入n个数,选择其中任意个数,使和最大且为奇 ...