Получить элемент, который был добавлен в DOM динамически, используя PURE JS - PullRequest
0 голосов
/ 18 апреля 2020

В основном, что говорит вопрос. Я импортирую код 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');
}

, но он не работает. Это должен быть ЧИСТЫЙ JS.

Я посмотрел, но не могу найти ответ: /

...