0

次の 2 つの HTML ドキュメントがあります。

Main.html

<html lang="eng">
<head>
  <title>JavaScript Example</title>
  <script type="text/javascript">

    var ExamId = "001A";

    function open_exam()
    {
      window.open("exam.html")
    }
  </script>
</head>
  <body>
    <input type=button value="Open Exam" onclick="open_exam()">
  </body>
</html>

Exam.html

<html lang="eng">
<head>
  <title>JavaScript Example</title>
  <script type="text/javascript">
    function setParentInfo()
    {
        window.parent.document.ExamID = '001B';
    }
  </script>
</head>
  <body>
    <p>Welcome to the Exam!</p>
    <input type=button value="Set Parent Info" onclick="setParentInfo()">
  </body>
</html>

Main.html は、入力ボタンを介して Exam.html を呼び出します。Exam.html 内から、親ドキュメント (つまり、Main.html) の変数 ExamID を変更したいと思います。JavaScript 関数 setParentInfo() を使用してこれを実行しようとしています。

上記のコードは機能しません。誰かが正しいコードを思いつくのを手伝ってくれますか?

本当にありがとう!

4

2 に答える 2

2

変数は、windowオブジェクトではなくオブジェクトに割り当てられdocumentます。

値はすでに設定されているため、代わりに既存の値を読み取って確認できます。

alert(window.parent.ExamId); // == "001A"
于 2012-07-08T04:46:46.137 に答える
0

変数は親ウィンドウで宣言および割り当てられるため、子ウィンドウから参照を取得できます。

alert次のステートメントを使用してテストできます。

alert(window.parent.document.ExamId);

//output::001B
于 2012-07-08T04:53:54.410 に答える