实际上,加载页面时发生了一个错误,您可以在控制台日志中找到类似的错误
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。