javascript - 使用 JS 将 HTML 块插入 HTML 文件
问题描述
我想使用 JS 将这个 HTML 块插入到另一个 HTML 文件中,因为如果我需要编辑导航,我可以在 JS 文件中编辑它:
<div class="navbar">
<div class="dropdown">
<div class="dropbtn" id="dropbtn">
<a id="menu"><img src="files/i/menu.svg" width="30"></img><span id="menu-text">Menü</span></a></div>
<div class="dropdown-content">
<a id="home" href="index.html"><img src="files/i/home.svg" width="30"></img><span>Startseite</span>
</a>
<a id="me" href="about.html"><img src="files/i/me.svg" width="30"></img><span>Über mich</span></a>
<a id="dienst" href="dienstleistungen.html"><img src="files/i/dienst.svg"
width="30"></img><span>Dienstleistungen</span> </a>
<a id="form" href="form.html"><img src="files/i/form.svg" width="30"></img><span>Anfrage</span></a>
</div>
</div>
</div>
最简单的方法是什么?我已经搜索过了,但我只看到了可以独自完成的事情。是否存在一种简单的方法来做到这一点?
我已经尝试过了,但我得到一个错误(Uncaught TypeError: Cannot set property 'innerHTML' of null: navbar.js:1
):
document.getElementById('first-body').innerHTML = `
<div class = "navbar" >
<div class = "dropdown" >
<div class = "dropbtn" id = "dropbtn" >
<a id = "menu" >
< img src = "files/i/menu.svg" width = "30" > < /img>
<span id="menu-text">Menü</span >
< /a>
</div >
<div class = "dropdown-content" >
<a id = "home" href = "index.html" >
<img src = "files/i/home.svg" width = "30" > < /img>
<span>Startseite</span >
</a>
<a id = "me" href = "about.html">
<img src = "files/i/me.svg" width = "30" > < /img>
<span>Übermich < /span>
</a >
<a id = "dienst" href = "dienstleistungen.html" >
< img src = "files/i/dienst.svg" width = "30" > < /img>
<span>Dienstleistungen</span >
< /a>
<a id = "form"href = "form.html" >
< img src = "files/i/form.svg" width = "30" > < /img>
<span>Anfrage</span >
< /a>
</div>
</div>
</div>
`;
在我要插入的 HTML 文件中,我使用这个:<script type="text/javascript" src="navbar.js"></script>
我的 HTML 文件:
<!DOCTYPE html>
<html lang='de'>
<head>
<link rel="stylesheet" href="style.css">
<link rel="shortcut icon" href="files/i/favicon.svg" />
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
</head>
<script type="text/javascript" src="navbar.js"></script>
<body id="first-body">
<!--here should be the navigation with js-->
<div class="body">
<!--content-->
</div>
</body>
</html>
有没有可能,我不能使用<body>
带有 ID 的标签?
如果我把<script>
之前或之后</body>
,它看起来像这样:
解决方案
您所缺少的只是一个 ID 为“first-body”的元素
document.getElementById('first-body').innerHTML = `
<div class = "navbar" >
<div class = "dropdown" >
<div class = "dropbtn" id = "dropbtn" >
<a id = "menu" >
< img src = "files/i/menu.svg" width = "30" > < /img>
<span id="menu-text">Menü</span >
< /a>
</div >
<div class = "dropdown-content" >
<a id = "home" href = "index.html" >
<img src = "files/i/home.svg" width = "30" > < /img>
<span>Startseite</span >
</a>
<a id = "me" href = "about.html">
<img src = "files/i/me.svg" width = "30" > < /img>
<span>Übermich < /span>
</a >
<a id = "dienst" href = "dienstleistungen.html" >
< img src = "files/i/dienst.svg" width = "30" > < /img>
<span>Dienstleistungen</span >
< /a>
<a id = "form"href = "form.html" >
< img src = "files/i/form.svg" width = "30" > < /img>
<span>Anfrage</span >
< /a>
</div>
</div>
</div>
`;
<! –– You didn't have an element with id of 'first-body' ––>
<div id="first-body">
</div>
推荐阅读
- python - 如何将逻辑公式转换为数组并计算?
- string - 在 AWK 中搜索多个字符串
- c# - 无法映射属性“PropertyName”,因为它属于“列表”类型
' - php - WordPress 上带有 SMTP 服务器的 phpmailer:带有 spamcore 8.5/10 的邮件无法到达 Gmail 或 Hotmail
- python - discord.py 中的打字机效果(我用谷歌翻译问这个问题,如有错误请见谅)
- javascript - 如何从缩略图 url 获取 Vimeo 视频 ID
- email - APEX 19.2 页面进程和会话状态
- r - 提取行导致空对象
- javascript - 运行时 Trie 数据结构生成与存储和读取文件方法
- r - 在 R 中,使用 read_html 读取的网站被重定向。如何获取重定向到的 url?