如何用JS操作iFrame里的Dom

如题,父页面的JS如何拿到iframe里的dom,并且进行操作? 原生JS,不用JQ之类的 页面代码:

<body>
    

<iframe id="container" name="container" src="frames/index.html"></iframe>

</body>
在chrome的console里:
 document.getElementById("container")

Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match. Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

<iframe id="container" name="container" src="frames/index.html">...</iframe>​

 document.getElementById("container").contentWindow.document

Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

undefined

 window.frames["container"].document

Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

undefined

虽然是相对路径,还是出现跨域的错误。

最后找到原因了,文件之间的操作是按照跨域处理的,把文件放在web容器里,127.0.0.1 去访问就好了

原文:http://segmentfault.com/q/1010000000199925
<think>好的,我现在要解决用户的问题:如何在IE浏览器中使用JavaScript访问和操作iframe中的DOM元素。根据提供的引用内容,我需要整理出具体的方法,并确保兼容性。 首先,用户提到在IE中可以通过document.frames["IframeName"]来获iframe对象,例如引用4中的例。同时,引用2指出在IE中,contentWindow属性可以省略,也就是说可以直接使用iframe元素来访问其contentWindow。这应该是关键点之一。 接下来,我需要对比IE和其他浏览器(如Firefox)的不同处理方式。根据引用2,Firefox必须使用contentWindow属性,而IE则可以省略。所以,为了兼容性,可能需要同时处理这两种情况。 然后,获iframe的window对象后,如何访问其DOM元素呢?通常的做法是通过contentDocument属性获文档对象,再使用标准方法如getElementById。但IE可能不支持contentDocument,这时候应该使用document.frames或者直接通过iframe元素来访问文档。 引用3中提到使用document.createElement等方法操作DOM,但这主要关注的是iframe的访问。用户的问题集中在访问和操作,比如获元素并修改内容。 还需要注意问题,但用户的问题可能是在同下,所以暂时不考虑限制。引用4提到JS访问iframeDOM,但可能涉及不同浏览器的安全策略,不过用户没有特别提到,所以可能先处理同情况。 总结步骤应该是: 1. 获iframe元素的引用,可以通过document.getElementById或document.frames。 2. 在IE中,使用document.frames或直接通过iframe元素获其contentWindow。 3. 通过contentWindow.document来访问iframe内的文档对象。 4. 使用标准DOM方法操作元素,如getElementById和innerHTML。 同时,要处理不同浏览器的兼容性问题,比如在IE中可能需要检查document.frames是否存在,或者是否支持contentWindow属性。此外,contentDocument在较旧的IE版本中可能不可用,需要用contentWindow.document代替。 测试代码的结构可能包括: - 通过id获iframe元素。 - 使用contentWindow或document.frames获window对象。 - 访问其document。 - 查找元素并修改内容。 还需要注意执行时机,确保iframe加载完成后再操作,可能需要监听load事件。 最后,给出示例代码,并说明在IE中的处理方式,以及兼容性写法。</think>在 Internet Explorer 中使用 JavaScript 操作 iframe 内的 DOM 元素,可通过以下步骤实现: --- ### 一、获 iframe 的 window 对象 1. **通过 `document.frames` 访问** IE 支持直接通过 `document.frames["iframe名称"]` 获 iframe 的 window 对象: ```javascript var iframeWindow = document.frames["myIframe"]; // myIframeiframe 的 name 属性值 ``` 2. **通过 DOM 元素访问** 若通过 `document.getElementById` 获 iframe 元素,需使用 `contentWindow` 属性(IE 可省略,但建议保留以兼容其他浏览器): ```javascript var iframe = document.getElementById("myIframe"); var iframeWindow = iframe.contentWindow || iframe; // IE 兼容处理 ``` --- ### 二、操作 iframe 内的 DOM 元素 1. **获文档对象** 通过 iframe 的 window 对象访问其文档: ```javascript var iframeDoc = iframeWindow.document; // IE 可直接使用 .document ``` 2. **修改元素内容** 使用标准 DOM 方法操作元素,例如修改 `innerHTML`: ```javascript var element = iframeDoc.getElementById("targetElement"); element.innerHTML = "修改后的内容"; ``` --- ### 三、完整示例代码 ```html <iframe name="myIframe" id="myIframe" src="inner.html"></iframe> <script> // 方法1:通过 document.frames (IE 兼容) var iframeWindow = document.frames["myIframe"]; // 方法2:通过 DOM 元素 (推荐浏览器) var iframe = document.getElementById("myIframe"); iframe.onload = function() { var iframeDoc = iframe.contentWindow?.document || iframe.contentDocument; var header = iframeDoc.getElementById("header"); header.innerHTML = "IE 中已修改标题"; }; </script> ``` --- ### 四、注意事项 1. **限制** 若 iframe 加载的是不同页面,浏览器会因安全策略禁止访问 DOM[^4]。 2. **加载时机** 操作 DOM 前需确保 iframe 内容已加载完成,可通过 `onload` 事件监听。 3. **兼容性优化** 使用 `contentWindow || contentDocument` 兼容旧版 IE 和其他浏览器[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值