javascript - 在我的主页的某个空间添加页面

标签 javascript html

这是我在 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/

相关文章:

javascript - 如何从属性中收集信息并将它们作为数组发送?

javascript - 简单的 SVG 路径不起作用

javascript - Angular/Laravel CORS 问题 : The Same Origin Policy disallows reading the remote resource

javascript - 查找与所选文本匹配的 DOM 选择器集

html - 通过 CSS 访问 html 内容?

jquery - 在javascript中计算以克服垂直对齐:middle

javascript - 根据 html 表中所做的选择更改 google 标记的颜色

javascript - 定制跨域iFrame

javascript - flutter 导航使上一个屏幕在底部可见

html - 无法让 ul 跨列