基本上问题说的是什么。我从外部文件导入 HTML 代码并将其附加到我的主 html 代码中:
/**
* Get the HTML contents from the URL specified in the filename and add them
* to the before the bottom of the specified DOM element.
* @param {string} filename The URL of the HTML file we want to import.
* @param {DOM element} element The element to which we'll append the HTML code.
*/
function importHtml(filename, element) {
let data = fetch(filename)
.then(function(response) {
// When the page is loaded convert it to text
return response.text()
})
.then(function(html) {
// Return the HTML contents of the file.
element.insertAdjacentHTML('beforeend', html);
})
.catch(function(err) {
console.log('Failed to fetch page: ', err);
});
return data;
}
importHtml("html/includes/header.html", header);
我尝试使用以下方法访问该 HTML 代码:
document.addEventListener("DOMContentLoaded", function () {
const menu = document.querySelector('#menubar');
}
但它不起作用。它必须是 PURE JS。
我看过,但似乎找不到答案:/