문제

<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로 iview 요소를 선택하려면 어떻게해야합니까? 감사.

도움이 되었습니까?

해결책

에서 이 페이지:

Mozilla는 iframeelm.contentDocument를 통해 iframe의 문서 개체에 액세스하는 W3C 표준을 지원하는 반면 Internet Explorer는 document.frames [ "name"]를 통해 액세스 한 다음 결과 문서에 액세스해야합니다.

따라서 브라우저를 감지해야하고 즉, 대신 이와 같은 작업을 수행해야합니다.

document.frames['iView'].document; 

다른 팁

크로스 브라우저는 contentDocument (Firefox 자체 포함 contentDocument 하다 일)입니다 contentWindow.document.

그래서 시도하십시오 :

alert(document.getElementById('iView').contentWindow.document);

contentWindow iframe에 대한 참조를 얻습니다 window 물체와 물론 .document Iframe의 DOM 문서 개체 일뿐입니다.

다음은 더 나은 기사가 있습니다.

당신은 iframe의 내용을 얻고 싶습니까?

IE7 및 FF2 :

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

기능 감지를 사용합니다 contentDocument IE 8에서 지원됩니다.

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
}
contentWindow.document.body.innerHTML

Internet Explorer와 Firefox에서 나를 위해 일하고 있지만

contentDocument.body.innerHTML

Firefox에서만 작동합니다.

다음과 같이하십시오 :

var myFrame = document.getElementById('iView');
var frameDoc = myFrame.contentDocument || myFrame.contentWindow;

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

alert(frameDoc);

보다 이 페이지 자세한 사항은

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top