一 在父页面操作子页面

 
IE下操作IFrame内容的代码:

document.frames["MyIFrame"].document.getElementById("s").style.color="blue";
但是这在Firefox下无效。
所以,想到在Firefox下用FireBug来调试。经过调试发现在Firefox下可用以下代码来实现:
 
document.getElementById("MyIFrame").contentDocument.getElementById("s").style.color="blue";

详细代码如下:
TestIFrame.htm:

<html>
<head>
<script type="text/javascript">
function f(){
var doc; if (document.all){//IE
doc = document.frames["MyIFrame"].document;
}else{//Firefox
doc = document.getElementById("MyIFrame").contentDocument;
} doc.getElementById("s").style.color="blue";
}
</script>
</head>
<body onload="f()"> <iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.htm" width = "100" height="100"></iframe> </body>
</html>

其实可以用一种通用方法:

<html>
<head>
<script type="text/javascript">
function f(){
//var doc = window.frames["MyIFrame"].document; 2中都可以
var doc = document.getElementById("MyIFrame").contentDocument;
doc.getElementById("s").style.color="blue";
}
</script>
</head>
<body onload="f()"> <iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "100" height="100"></iframe> </body>
</html>

二 在子页面中操作父页面

父页面操作子页面

<html>
<head>
</head>
<body> <h2 id="s">55555555555555555555555555</h2>
<script>
       //window.parent是获得父窗口的window对象
window.parent.document.getElementById("txt").innerHTML="你猜怎么样"; </script>
</body>
</html>

三 在父页面调用子页面的函数

<html>
<head> </head>
<body>
<iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "100" height="100"></iframe>
<script type="text/javascript">
window.onload=function(){
//window.frames["MyIFrame"].window.test();
document.getElementById("MyIFrame").contentWindow.test();
}
</script>
</body>
</html>

四  在子页面调用父页面的函数

<html>
<head>
</head>
<body>
<h2 id="s">55555555555555555555555555</h2>
<script type="text/javascript">
window.onload=function(){
window.parent.test();
}
</script>
</body>
</html>

总结:iframe中的src是不能跨域的,如果跨域则无法进行操作。

js操作iframe总结的更多相关文章

  1. 原生JS操作iframe里的dom

    转:http://www.css88.com/archives/2343 一.父级窗口操作iframe里的dom JS操作iframe里的dom可是使用contentWindow属性,contentW ...

  2. JS 操作iframe

    很多人一直都有个想法,要是可以随心所欲的操作iframe就好了.这样静态页面也就有了相当于后台动态页面php,jsp,asp中include,require实现统一多页面布局的能力. 通过Javasc ...

  3. [置顶] js操作iframe兼容各种浏览器

    在做项目时,遇到了操作iframe的相关问题.业务很简单,其实就是在操作iframe内部某个窗体时,调用父窗体的一个函数.于是就写了两个很简单的htm页面用来测试,使用网上流行的方法在谷歌浏览器中始终 ...

  4. JS操作iframe

    1. 获得iframe的window对象 存在跨域访问限制. chrome:iframeElement. contentWindow firefox: iframeElement.contentWin ...

  5. JS操作iframe元素

    1.  demo1.html页面中有个iframe元素,iframe元素的src是iframe1.html,怎么在demo1.html页面中操作iframe1.html页面 答曰:demo1.html ...

  6. js操作iframe框架时应该屡清楚的一些概念

    1.获取iframe的window对象 存在跨域访问限制. iframeElement.contentWindow 兼容 2.获取iframe的document对象 存在跨域访问限制. chrome: ...

  7. js操作Iframe非当前最上层窗体

    如果当前窗口不是最上层窗口(比如是在Iframe中),那么就把自己变为最上层窗口.  <script language="javascript" type="tex ...

  8. 百度地图和js操作iframe

    document.getElementById("ifarme-63").contentWindow.document.getElementById("qksv" ...

  9. JS操作未跨域iframe里的DOM

    这里简单说明两个方法,都是未跨域情况下在index.html内操作b.html内的 DOM. 如:index.html内引入iframe,在index内如何用JS操作iframe内的DOM元素? 先贴 ...

随机推荐

  1. Swift中文手册 -- The Basics

    原文:Swift中文手册 -- The Basics 基础部分 Swift 是 iOS 和 OS X 应用开发的一门新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 S ...

  2. 实战parse_ini_file()及扩展函数解析ini文件完整版

    文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/587 在PHP站点开发的过程中,往往会用到读取ini參数配置文件,比方须要訪问一些复杂的借 ...

  3. C++中public,protected,private访问

    对于公有继承方式: (1)父类的public成员成为子类的public成员,允许类以外的代码访问这些成员:(2)父类的private成员仍旧是父类的private成员,子类成员不可以访问这些成员:(3 ...

  4. Coreseek/sphinx全文检索的了解

    Coreseek/sphinx全文检索的了解 概述: 全文检索是一种将文件里全部文本与检索项匹配的文字资料检索方法,全文检索是将存储于数据库中整本书.整篇文章中的随意内容信息查找出来的检索.它能够依据 ...

  5. UVA The Sultan&#39;s Successors

    题目例如以下: The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that thecou ...

  6. mvc之验证IEnumerable<T> 类型,多选框验证

    原文:mvc之验证IEnumerable<T> 类型,多选框验证 假设我们有这么一种需求,我们要同时添加年级和年级下面的多个班级,我们一般会像下面这种做法. Action中我们这样接收: ...

  7. UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)

    在本人之前的一篇文章<<Appium基于安卓的各种FindElement的控件定位方法实践和建议>>第二章节谈到Appium可以通过使用UIAutomator的方法去定位And ...

  8. asp.net读取CSV

    原文:asp.net读取CSV 用Excel导了两天数据,各种问题,折磨客户也折磨了自己,以前没发现的问题一下子都暴露出来了 特意收集两篇Excel跟CSV读取相关的两篇文章 asp.net读取exc ...

  9. Kindle使用技巧

    Kindle使用技巧 使用kindle也有几年了,但是好多kindle的使用技巧还不知道,在网上看到了一些,整理了一下. 1. 格式转换 把PDF发送到Kindle的时候,邮件主题写convert,这 ...

  10. Fluent Validation

    .NET业务实体类验证组件Fluent Validation   认识Fluent Vaidation. 看到NopCommerce项目中用到这个组建是如此的简单,将数据验证从业务实体类中分离出来,真 ...