с псевдодом ниже я получаю несколько слушателей - то же самое для каждого элемента.
Как сделать это, чтобы получить один EventListener - и как получить его контекст? class = "Snippet">
Код: Выделить всё
const functionA = (el) => {
const items = 1;
console.log('functionA');
const functionB = (items) => {
console.log('functionB');
}
document.addEventListener('click', function (e) {
const $this = e.target;
console.log($this);
functionB(items);
});
}
Array.from(document.querySelectorAll('.el')).forEach((el) => {
functionA(el);
});< /code>
El 1
El 2
El 3Подробнее здесь: https://stackoverflow.com/questions/796 ... de-foreach
Мобильная версия