如何将Ajax回调分配给脚本

时间:2012-02-21 10:32:25

标签: php ajax

<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;这是错误的。

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

1 个答案:

答案 0 :(得分:2)

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

你应该使用

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