HTML5 在 index.html 中导入外部本地 html 文件

Posted

技术标签:

【中文标题】HTML5 在 index.html 中导入外部本地 html 文件【英文标题】:HTML5 Importing external local html file in index.html 【发布时间】:2017-03-14 13:33:13 【问题描述】:

早些时候,我设法从我的 index.html 的外部 html 文件中加载 html 代码

<head>
    <link rel="import" href="html/html_snippets.html">
</head>

然后用

找到它
var mySnippet = $(".foo").html();

但现在我开始收到有关 CORS 政策的错误:

Access to Imported resource at 'file:///path/html/html_snippets.html' from origin 'file://' has been blocked by CORS policy: Invalid response. Origin 'null' is therefore not allowed access.

我无法让它工作。

我怎样才能覆盖它,我需要在外部文件中有 html sn-ps 并在启动时加载它们一次,然后再处理它们。

【问题讨论】:

这可以帮助***.com/questions/27222306/… 这个问题和答案对我没有帮助,我需要在本地拥有文件,并且不想对所有内容都使用外部库... 【参考方案1】:

是的,HTML 可以选择包含 html 文件。语法是这样的

<div w3-include-html="content.html"></div>

<script>
w3IncludeHTML();
</script>

供参考http://www.w3schools.com/howto/howto_html_include.asp

【讨论】:

这似乎有效,但我真的不想使用另一个外部脚本来完成应该简单的事情

以上是关于HTML5 在 index.html 中导入外部本地 html 文件的主要内容,如果未能解决你的问题,请参考以下文章

在 index.html 中导入 css 和在 Angular 5 中导入 styleUrls 的区别

无法加载模块脚本 - 在 index.html 中导入类

如何在 Vue.js 中导入 css?

如何在 Vue 组件中导入外部函数?

json 尝试在打字稿中导入外部模块

在 Hadoop MapReduce 脚本中导入外部库