【发布时间】:2021-03-12 11:43:00
【问题描述】:
我正在尝试从放置在同一目录中的单独文件加载 HTML 标记。为此,我正在关注这篇文章:HTML Includes That Work Today。
我有两个文件,内容如下:page2.html:
<html>
<style>
div {
display: inline-block;
background-color: #0902;
border: 2px solid green;
border-radius: 10px;
padding: 20px;
font-size: 1.2em;
}
</style>
<body>
<div id="x">Contents</div>
</body>
</html>
test.html:
<html>
<body>
<iframe src="page2.html" style="display: none;" onload="alert('onload event called'); this.before(this.contentDocument.body.children[0]); this.remove();"></iframe>
</body>
</html>
我希望将page2.html 的内容附加到当前文档中。但是,我得到:
Uncaught TypeError: Cannot read property 'body' of null
at HTMLIFrameElement.onload (test.html:10)
我犯了什么错误?
【问题讨论】:
标签: javascript html