Javascript IE中的getElementById.contentDocument错误 函数func(){ 警报(document.getElementById('iView').contentDocument); }

Javascript IE中的getElementById.contentDocument错误 函数func(){ 警报(document.getElementById('iView').contentDocument); },javascript,explorer,getelementbyid,Javascript,Explorer,Getelementbyid,单击后,Firefox返回[object HTMLDocument]。Internet Explorer返回未定义的 如何使用Internet Explorer选择iView元素?谢谢。您似乎想正确获取iframe的内容 IE7和FF2: <html> <script type="text/javascript"> function func() { alert(document.getElementById('iView').cont

单击后,Firefox返回[object HTMLDocument]。Internet Explorer返回未定义的


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

您似乎想正确获取iframe的内容

IE7和FF2:

<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>
发件人:

Mozilla支持通过IFrameElm.contentDocument访问iframe的文档对象的W3C标准,而Internet Explorer要求您通过document.frames[“name”]访问它,然后访问生成的文档

因此,您需要检测浏览器并在IE上执行以下操作:

var iframe = document.getElementById('iView');
alert(iframe.contentWindow.document.body.innerHTML);

相当于
contentDocument
(包括Firefox本身,
contentDocument
可以工作)的跨浏览器是
contentWindow.document

因此,请尝试:

document.frames['iView'].document; 
contentWindow
获取对iframe的
window
对象的引用,当然
.document
只是iframe的DOM文档对象


.

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

alert(document.getElementById('iView').contentWindow.document);
在Internet Explorer和Firefox中为我工作

contentWindow.document.body.innerHTML

只能在Firefox中使用。

执行以下操作:

contentDocument.body.innerHTML

更多详情请参见

@Mauris:他很有礼貌,只问了10个问题。他将有时间在这里学习规则。对那些提出了100多个问题但从未接受回答或回答过问题的人发表评论。对不起,我不知道这个系统有“接受”功能,谢谢:)帮助我解决了IE7的问题。较新的IE版本似乎支持contentDocument。
contentDocument.body.innerHTML
var myFrame = document.getElementById('iView');
var frameDoc = myFrame.contentDocument || myFrame.contentWindow;

if (frameDoc.document){
  frameDoc = frameDoc.document;
}

alert(frameDoc);