javascript - JS DOM insertBefore() 无法正常工作
问题描述
我想在网站页脚之前在 JS DOM 中添加一个 div。但是,它不起作用(它将 div放在页脚之后)。
const div = document.createElement('div');
div.innerHTML = "Hello!";
const footer = document.getElementById('footer');
document.body.insertBefore(div, footer);
HTML:
<footer id="#footer">This is footer</footer>
我之前添加了 .js 文件</body>
。
解决方案
页脚中的 id 有一个额外的“#”,因此 Js 选择器无法正常工作。尝试删除它:
<footer id="footer">This is footer</footer>
推荐阅读
- r - 如何在没有数据的情况下做出未来预测?(右)
- azure-ad-b2c - Azure B2C - 找不到 ID 为 \"objectId\" 的查找声明的声明
- c - 有人可以解释一下这个程序的输出是怎样的吗
- streaming - grpc 服务器端流式传输失败(带有子序列 CORS 错误)
- sql - 为什么 QUOTE_IDENT 将嵌入的引号加倍
- c - 为什么我在第 57 行的读取打印了已经在使用的 errno 资源?
- python - 在 python 中导入 SentenceTransformer 时出错
- flutter - PlatformException(popup_blocked_by_browser,从 GoogleAuth.signIn() 引发的异常
- node.js - 文本 JSON 日志到由 node.js console.log() 生成的真实 json 解析
- keras - 使用 SVM 分类器作为预训练模型的最后一层(VGG16)