【发布时间】:2021-10-08 18:32:35
【问题描述】:
我正在尝试使用 JS 设置 Font Awesome 页脚,以便可以将其添加到页脚,但无法添加,因为它显示为 [object HTMLElement] 2020 - 2021。我已经导入了 Font Awesome 的脚本,但它不起作用。
我希望页脚显示为 © 2020 - 2021,但改用 Font Awesome 徽标。
document.body.onload = footer;
function footer() {
// create a new div element
const footerDiv = document.createElement("footer");
// assign it a class
footerDiv.classList.add("footer");
// gets the current date
const copyright = new Date().getFullYear();
// gets the copyright symbol
const favicon = document.createElement("i");
favicon.classList.add("fas.fa-copyright");
const completedFooter = document.createTextNode(favicon +
" 2020 " + "- " + copyright);
// add the text node to the newly created div
footerDiv.appendChild(completedFooter);
// add the newly created element and its content into the DOM
const newDiv = document.getElementById("div");
document.body.insertBefore(footerDiv, newDiv);
}
<head>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/js/all.min.js"> </script>
</head>
【问题讨论】:
-
因为你不能同时添加一个DOM元素和一个字符串......你应该追加元素,而不是在文本节点中设置它