IEでgetElementById.contentDocumentエラー
-
16-09-2019 - |
質問
<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);
を参照してください。詳細については、このページ
所属していません StackOverflow