在div中加载html和php页面

在div中加载html和php页面,php,javascript,ajax,Php,Javascript,Ajax,单击按钮时如何在div中加载html 我有功能,但不起作用: function ajaxFunction(id, url){ document.getElementById("sh").style.visibility = 'hidden'; var xmlHttp; try {// Firefox, Opera 8.0+, Safari xmlHttp = new XMLHttpRequest(); } catch (e) {// Intern

单击按钮时如何在div中加载html

我有功能,但不起作用:

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);
}
函数ajaxFunction(id,url){
document.getElementById(“sh”).style.visibility='hidden';
var-xmlHttp;
试试{//Firefox、Opera8.0+、Safari
xmlHttp=新的XMLHttpRequest();
}捕获(e){//Internet Explorer
试一试{
xmlHttp=新的ActiveXObject(“Msxml2.xmlHttp”);
}捕获(e){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}捕获(e){
警告(“您的浏览器不支持AJAX!”);
返回false;
}
}
}
xmlHttp.onreadystatechange=函数(){
if(xmlHttp.readyState==4){
//从服务器获取响应并提取第二个html页面主体部分中的部分避免在第一个页面的元素中插入第二个页面的标题部分
var respText=xmlHttp.responseText.split(“”);
elem.innerHTML=respText[1]。拆分(“”)[0];
}
}
var elem=document.getElementById(id);
如果(!elem){
警报(“页面中不存在具有传递ID的元素”);
返回;
}
open(“GET”,url,true);
xmlHttp.send(空);
}
我有一个按钮可以点击它

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

当我放置newmetere.php时,函数不起作用并获取垃圾值。

试试看:这是一种使用Ajax在网页中异步加载另一页内容的快速而简单的方法

我讨厌成为“那个家伙”,但你可能想看看jQuery。在任何人生气之前,这意味着您直接进入了现代教程,jQuery鼓励事件绑定而不是onclick类型的东西。您提到newmetere.php无法加载,但我假设您的示例中的.html扩展可以工作。要点很简单,但您确定您的服务器正确地服务于.php吗?您可以直接访问newmetere.php页面吗?那里的输出是什么?你百分之百确定没有打字错误吗?