javascript 中contentWindow和 frames和iframe之间通信

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>

原文地址:https://www.cnblogs.com/suizhikuo/p/3228938.html