代码之家  ›  专栏  ›  技术社区  ›  S O N  J O ツ

我的HTML代码片段内容不会显示在屏幕上

  •  0
  • S O N  J O ツ  · 技术社区  · 4 年前

    有人能给我解释一下HTML代码片段吗?请参见下图(W3HTML代码片段教程)。然后我根据w3schools的说明创建一个“content.html”文件。然后我创造了另一个。html文件,然后使用相同的代码,但不向显示器显示任何内容。有人能给我解释一下怎么用吗?

    enter image description here

    当我试着打开我的电脑-

    enter image description here

    enter image description here

    0 回复  |  直到 4 年前
        1
  •  1
  •   Kassim Balogun    4 年前

    实际上,加载页面时发生了一个错误,您可以在控制台日志中找到类似的错误

    html。html:1

    在'file:///content.htmlCORS策略已阻止“来自源”的“null”:跨源请求仅支持协议方案:http、数据、chrome扩展、edge、https、chrome untrusted。

    由于没有web服务器,因此使用文件协议而不是HTTP协议加载文件时会出现这种情况。因为浏览器不允许您使用HTTP请求使用文件协议加载本地文件。下面的代码就是这么做的。

    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4) {
            if (this.status == 200) {elmnt.innerHTML = this.responseText;}
            if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
            /*remove the attribute, and call this function once more:*/
            elmnt.removeAttribute("w3-include-html");
            includeHTML();
        }
    } 
    

    要在本地系统上运行,您必须设置一个web服务器。并使用localhost访问HTML文件。根据您实现此功能的意图,您可以寻找合适的web服务器。不过,我建议Apache服务器使用相对容易安装的XAMPP。