2

これは、JavaScript の専門家にとって非常に単純なものに違いありません。次のコードでは、ブラウザー ウィンドウの高さいっぱいまで iframe を開こうとしています。

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
         <script language="javascript" type="text/javascript">
    function resizeIframe() {
    var height = document.documentElement.clientHeight;
    height -= document.getElementById('documentFrame2').offsetTop;
    height -= 20;
    document.getElementById('documentFrame2').style.height = height +"px";
 };
document.getElementById('documentFrame2').onload = resizeIframe;
window.onresize = resizeIframe;
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <iframe src="standard-tube-map.pdf" id="documentFrame2" width="100%" onload="resizeIframe();" ></iframe> 
    </div>
    </form>
</body>
</html>

Mozilla では動作しますが、IE では (IE8 でのみテストされています)、「document.getElementById(...) は null またはオブジェクトではありません」というエラーが発生します。クロスブラウザで動作させるために何を変更すればよいか、誰か提案できますか?

どうもありがとう、アリ

4

4 に答える 4

2

ラッピングしてみる

document.getElementById('documentFrame2').onload = resizeIframe;

内部:

window.onload = function(){

    document.getElementById('documentFrame2').onload = resizeIframe;
    window.onresize = resizeIframe;
    resizeIframe();
};

ページ/Dom が読み込まれるまで、iframe は DOM に存在しません。

更新

投稿したコードの一部が表示されませんでした。

onload = "resizeIframe"要素自体内に保持し、JavaScript を次のように head に含めることができます。

function resizeIframe() {
    var height = document.documentElement.clientHeight;
    height -= document.getElementById('documentFrame2').offsetTop;
    height -= 20;
    document.getElementById('documentFrame2').style.height = height +"px";
 }


 window.onresize = resizeIframe;

そうすれば、要素の後に JavaScript を実行する必要がなくなります。

于 2010-06-30T08:39:38.880 に答える
1
  1. alert(document.getElementById('abcId').value);
    
  2. alert(document.formName.elements['abcName'].value);
    

私は同じ問題を経験し、2番目の問題を試しました。

于 2011-10-26T18:21:31.167 に答える
0

あなたがやる

document.getElementById('documentFrame2').onload = resizeIframe;

ドキュメントの準備が整う前に、iframe を見つけることができます。onload- または document-ready-function または iframe の後の別の javascript-block でこれを呼び出します。

于 2010-06-30T08:39:03.827 に答える
0

iframe タグで使用したことがある場合は、もうonload"resizeIframe"必要ありませんdocument.getElementById('documentFrame2').onload = resizeIframe;

私は次の方法で置き換えることを好みますwindow.onresize = resizeIframe;

window.onload = function() {
  window.onresize = resizeIframe;
};

iframe の操作準備が整う前にウィンドウのサイズが変更されることがあり、エラーが発生することがあります。

========================

編集:

window.onload = function() {
  resizeIframe();
  window.onresize = resizeIframe;
};
于 2010-06-30T08:45:06.423 に答える