博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript 中contentWindow和 frames和iframe之间通信
阅读量:7237 次
发布时间:2019-06-29

本文共 1904 字,大约阅读时间需要 6 分钟。

iframe父子兄弟之间通过jquery传值(contentWindow && parent),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进行操作;

 

 

ie 中为 frames["id"]其他为document.getElementById("id").contentWindow

contentWindow属性是指指定的frame或者iframe所在的window对象

在IE中iframe或者frame的contentWindow属性可以省略,但在Firefox中如果要对iframe对象进行编辑则
必须指定contentWindow属性。
function EnableEdit()
{
      var editor;
      editor = document.getElementById("HtmlEdit").contentWindow;
   // 针对IE浏览器, make it editable
      editor.document.designMode = 'On';
      editor.document.contentEditable = true;
   // For compatible with FireFox, it should open and write something to make it work
editor.document.open();
editor.document.writeln('<html><head>');
editor.document.writeln('<style>body {background: white;font-size:9pt;margin: 2px; padding: 0px;}</style>');
editor.document.writeln('</head><body></body></html>');
editor.document.close();
}
<iframe   ID="HtmlEdit" MARGINHEIGHT="1" MARGINWIDTH="1" width="100%" height="312">
</iframe>
<html>
<body>
<script>
var ifr = document.createElement("iframe");
document.body.appendChild(ifr);
var ifrdoc = ifr.contentWindow.document;
var s = fixingHTB.innerHTML;   //进入可编辑模式前存好
ifrdoc.designMode = "on";     //文档进入可编辑模式
ifrdoc.open();                        //打开流
ifrdoc.write(s); 
ifrdoc.close();                        //关闭流
ifrdoc.designMode ="off";     //文档进入非可编辑模式
</script>
</body>
</html>

转载地址:http://fngfm.baihongyu.com/

你可能感兴趣的文章
Python3类型提示
查看>>
websocket多线程问题
查看>>
从上往下打印二叉树
查看>>
决策支持系统是什么?
查看>>
解压缩
查看>>
Linux中curl命令和wget命令的使用介绍与比较
查看>>
shell之awk 记录
查看>>
python内置数据结构之set
查看>>
function_score 之script_score
查看>>
ssh配置客户端免密钥到服务端
查看>>
启动PHP时提示初始化错误
查看>>
复杂recyclerView封装库
查看>>
Java通过POI为Excel添加数据验证
查看>>
140925左右发现的bash重大漏洞
查看>>
Animation 作为ImageView的背景
查看>>
mysql 主从备份实验
查看>>
CentOS下用命令查看IP地址
查看>>
LNMP编译安装遇到问题归总
查看>>
PHP的bbs实现之四--创建调查
查看>>
搭建windows下filezilla FTP服务器
查看>>