iframe父子兄弟之间调用传值(contentWindow && parent)
2016-06-22 16:56
423 查看
学习一个知识点最好的方法就是自己动手去的实现一些简单的helloworld,虽然可以在网上可以找到很多总结,但自己实现的过程才是自己的,期间遇到问题并解决问题的过程也会让自己记忆理解更加深刻。
更多信息,请参考:http://www.cxyeye.com/
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)
主页面如何调用子页面中的方法;
子页面如何调用主页面中的方法;
两个子iframe之间如何如何进行交互
main.html
frame.htm
兄弟iframe页面 newIframe.htm
结语:
我对jquery html css的理解还需要很多提高,自己总结的东西对大牛来说也很幼稚,但大牛之路都是如此总结向上提高的,我不怕这种幼稚。
更多信息,请参考:http://www.cxyeye.com/
更多信息,请参考:http://www.cxyeye.com/
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)
主页面如何调用子页面中的方法;
子页面如何调用主页面中的方法;
两个子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>
结语:
我对jquery html css的理解还需要很多提高,自己总结的东西对大牛来说也很幼稚,但大牛之路都是如此总结向上提高的,我不怕这种幼稚。
更多信息,请参考:http://www.cxyeye.com/
相关文章推荐
- 解决dubbo问题:forbid-consumer/
- Java多线程之键盘操作练习
- Oracle STA的使用
- WebRtc VoiceEngine代码解析
- 华为机试---蘑菇阵(动态规划)
- 四:深入Nginx之事件和连接 (之三:连接池)
- 文章标题
- 插件记录
- Android热修复学习(一)
- 关系型数据库之主从复制应用
- iOS NSString retainCount遇到的问题
- 【Leetcode344】. Reverse String-题解
- ps制作精美的书籍封面
- JSP
- 四层和七层负载均衡的区别
- java字符串分解 StringTokenizer用法
- 记录 Linux环境下java web项目CPU爆表 “事故”,肇事者:GC
- Android Wi-Fi Display(Miracast)介绍
- 多表查询(表连接)
- [转]Android开发:Parallax效果的ScrollerView,改编自ParallaxListView