質問

<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は、[オブジェクトのHTMLDocument]を返します。 Internet Explorerは、undefinedを返します。

どのように私は、Internet ExplorerでのiView要素を選択することができますか?おかげます。

役に立ちましたか?

解決

このページから:

  Internet Explorerは、結果のドキュメントにアクセスし、[「名前」]のDocument.Framesを介してアクセスし、する必要がありながら、

Mozillaは、IFrameElm.contentDocument通じIFRAMEのドキュメントオブジェクトにアクセスするためのW3C標準をサポートしています。

ブラウザを検出し、IE上のような何かをする必要があるので、この代わります:

document.frames['iView'].document; 

他のヒント

contentDocument 動作しFirefoxの自体を含む)contentDocumentするクロスブラウザ当量contentWindow.documentある。

そうしようとします:

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

contentWindowはあなたのiframeのwindowオブジェクトへの参照を取得し、もちろん.documentはiframe対応だけでDOM Documentオブジェクトです。

はここより良いにまとめた記事です。

あなたは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