这是我在 stackoverflow 上的第一个问题。
我想在主页的某个空间加载不同的页面。我用 JavaScript 尝试了很多次,但都碰壁了。我可以用 iframe 做到这一点。但我在想是否还有其他方法可以做到这一点。我想在用户单击链接时加载页面。
我的做法是这样的:
document.getElementById('aboutUs').onclick = function() {
document.getElementById('newDiv').style.display = "none";
document.getElementById('iFrame').style.display = "block";
}
谢谢!!!
最佳答案
您需要发出请求来加载静态内容。
希望这个片段有用
//A generic function to load the static page
function loadContent(target, staticPageLoc) {
var r = new XMLHttpRequest(); // making request to load the static page
r.open("GET", staticPageLoc, true);
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
target.innerHTML = ""; // removing any previous content
target.innerHTML = r.responseText; // response will be the static page
};
r.send();
}
document.getElementById('aboutUs').onclick = function() {
loadContent(document.getElementById('id of dom element where page will load'), 'page path')
}
或者,您可以探索 jquery.load
函数,并更好地在服务器中运行它,否则您可能会遇到 CORS
关于javascript - 在我的主页的某个空间添加页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39423011/