首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >IE中的getElementById.contentDocument错误

IE中的getElementById.contentDocument错误
EN

Stack Overflow用户
提问于 2009-09-25 14:15:09
回答 4查看 25.9K关注 0票数 18
<html>
   <script type="text/javascript">
      function func() {
         alert(document.getElementById('iView').contentDocument);
      }    
   </script>
   <body>
      <iframe id="iView" style="width:200px;height:200px;"></iframe>
      <a href="#" onclick="func();">click</a>
   </body>
</html>

单击后,Firefox返回object HTMLDocument。Internet Explorer返回undefined。

如何使用Internet Explorer选择iView元素?谢谢。

EN

回答 4

Stack Overflow用户

发布于 2009-09-25 14:20:48

您似乎想要获取iframe的内容,对吗?

IE7和FF2:

var iframe = document.getElementById('iView');
alert(iframe.contentWindow.document.body.innerHTML);
票数 3
EN

Stack Overflow用户

发布于 2009-09-25 14:54:30

使用功能检测,因为IE 8支持contentDocument

var iframe = document.getElementById("iView");
var iframeDocument = null;
if (iframe.contentDocument) {
    iframeDocument = iframe.contentDocument;
} else if (iframe.contentWindow) {
    // for IE 5.5, 6 and 7:
    iframeDocument = iframe.contentWindow.document;
}
if (!!iframeDocument) {
    // do things with the iframe's document object
} else {
    // this browser doesn't seem to support the iframe document object
}
票数 3
EN

Stack Overflow用户

发布于 2012-04-04 13:06:40

contentWindow.document.body.innerHTML

在Internet Explorer和Firefox中为我工作,而

contentDocument.body.innerHTML

只能在Firefox中工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1477547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档