如何将 Ajax 回调分配给脚本


How to assign Ajax callback to the script

<html>
  <head>
  <script>
  function sentAjax()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
        //alert(xmlhttp.readyState);
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementsByName('content') = xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","resultPage.php",true);
    xmlhttp.send();
    }
  </script>
  <script name="content">
     //should be assigned here
  </script>
  </head>
  <body>
  </body>
</html>

下面是"resultPage.php"的结果

<script type="text/javascript">
    objTreeMenu_1.drawMenu();
    objTreeMenu_1.writeOutput();
    objTreeMenu_1.resetBranches();
</script>

我想将上述脚本分配给内部<script name="content"></script>。但是,我已经厌倦了错误的document.getElementsByName('content') = xmlhttp.responseText;

有人可以帮助我吗?非常感谢!

document.getElementsByName('content')只返回元素。

你应该使用

document.getElementsByName('content')[0].innerHTML = xmlhttp.responseText