我用来导入外部 HTML 的脚本,该脚本具有基于 Bootstrap 的导航栏,这是 Javascript:
<script type="text/javascript">
var link = document.querySelector('link[rel="import"]');
// Clone the <template> in the import.
var template = link.import.querySelector('template');
var clone = document.importNode(template.content, true);
document.querySelector('#navBar').appendChild(clone);
</script>
在我的 HTML 页面中,我所做的就是在部分中包含对外部 html 文件的引用,并在我的部分中添加一个具有定义的类名的 div,如下所示:
<!— link of external HTML file in <HEAD> section -->
<link rel="import" href="navbar.html">
<!— Div with defined class name “navBar”in <BODY> section -->
<div id="navBar"></div>
最佳答案
HTML5?互联网爆炸者?
https://caniuse.com/#feat=imports
不。
关于包含外部 HTML 的 JavaScript 在 Chrome 中有效,但在 Internet Explorer 和 Firefox 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49352173/