2012-02-21 24 views
0
<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」的結果如何Ajax回調分配給腳本

<script type="text/javascript"> 

    objTreeMenu_1.drawMenu(); 
    objTreeMenu_1.writeOutput(); 
    objTreeMenu_1.resetBranches(); 

</script> 

我想上面的腳本分配給內部<script name="content"></script>。但是,我已經厭倦了document.getElementsByName('content') = xmlhttp.responseText;這是錯誤的。

有沒有人可以幫助我?非常感謝!

回答

2

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

你應該使用

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