1

ホームページ用に設計されたjsファイルに取り組んでいます。ナビゲーション メニュー バーを使用して、このページから他のページに移動したいと考えています。ターゲット ページは同じテンプレート (html コード) を共有しているため、特定のページに移動するには、xml ファイルに保存されている特定のコンテンツを読み込み、そのコンテンツをターゲット ページに渡す必要があります。

function loadFileToElement(filename, elementId)
{
  var xhr = new XMLHttpRequest();
  try
  {
    xhr.open("GET", filename, false);
    xhr.send(null);
  }
  catch (e) {
    window.alert("Unable to load the requested file.");
   }
  // Until this point I can load the specific content
  // How can I get from the url of the target page
  // a js document object, so that I can call getElementById(Id)
  // to pass the specific content.
  // For instance: Im currently opnening X1:= www.main.com 
  // und I would like to switch to X2 := www.targetpage.com 
  // target page which contains html the templat.
  //  The problem **document** represents currently X1
  // but i would like to set it to X2 so that I can pass
  // the content of xhr.responseText to it
  var component = **document**.getElementById(elementId);
  component.innerHTML = xhr.responseText;
  } 

Thanks
4

2 に答える 2

0

xhr.onloadこれを試してください。応答から返されたテキストを入力する関数を追加しました

function loadFileToElement(filename, elementId)
{
    var xhr = new XMLHttpRequest();
    try
    {
        xhr.open("GET", filename, false);
        xhr.onload = function () {
            var component = document.getElementById(elementId);
            component.innerHTML = xhr.responseText;
        }
        xhr.send(null);
    }
    catch (e) {
        window.alert("Unable to load the requested file.");
    }
} 

onreadystatechangeの代わりにイベントを使用することもできますonloadクリックして詳細を参照

于 2013-08-06T10:45:37.150 に答える