在div中加载html和php页面

时间:2013-03-12 16:26:42

标签: 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) {// 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函数不起作用并获得垃圾值。

1 个答案:

答案 0 :(得分:0)

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