2013-03-12 102 views
0

当我点击一个按钮时,如何在div中加载html?在div中加载html和php页面

我有功能,但它不工作:

function ajaxFunction(id, url){ 
document.getElementById("sh").style.visibility = 'hidden'; 
    var xmlHttp; 
    try {// Firefox, Opera 8.0+, Safari 
     xmlHttp = new XMLHttpRequest();  
    } catch (e) {// Internet Explorer 
     try { 
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try { 
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e) { 
       alert("Your browser does not support AJAX!"); 
       return false; 
      } 
     } 
    } 

    xmlHttp.onreadystatechange = function(){ 
     if (xmlHttp.readyState == 4) { 
      //Get the response from the server and extract the section that comes in the body section of the second html page avoid inserting the header part of the second page in your first page's element 
      var respText = xmlHttp.responseText.split('<body>'); 
      elem.innerHTML = respText[1].split('</body>')[0]; 
     } 
    } 

    var elem = document.getElementById(id); 
    if (!elem) { 
     alert('The element with the passed ID doesn\'t exists in your page'); 
     return; 
    } 

    xmlHttp.open("GET", url, true); 
    xmlHttp.send(null); 
} 

而且我有按钮,点击它。

<button type="button" name="new metere" value="" class="button" onclick="ajaxFunction('test','newmetere.html');">new metere</button> 

当我把newmetere.php功能不起作用,并获得垃圾值。

+0

我讨厌成为'那个人',但你可能想看看jQuery。在任何人生气之前,这意味着你直接进入现代教程,jQuery鼓励事件绑定而不是onclick类型的东西。 – 2013-03-12 16:29:26

+0

你提到newmetere.php无法加载,但我假设您的示例工程中的.html扩展名。简单的点,但你确定你的服务器正在服务.php?你能直接访问newmetere.php页面吗?那里的产量是多少?你100%肯定没有错别字? – TommyBs 2013-03-12 16:35:19

回答

0

尝试jQuery's load():这是一种使用Ajax在网页中异步加载另一页内容的快速简单方法!