简体   繁体   English

在div中加载html和php页面

[英]Loading html and php page in div

How can I load html in a div when I click a button? 单击按钮时如何在div中加载html?

I have function but it does not work: 我有功能,但是不起作用:

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);
}

And I have button to click it. 我有按钮可以单击它。

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

when I put newmetere.php Function does not work and get garbage value. 当我放newmetere.php函数不起作用并获得垃圾值。

尝试使用jQuery的load() :这是使用Ajax从网页中异步地从另一个页面加载内容的快速简便的方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM