有没有办法将元素和实例传递给事件处理程序回调?
listNotes(key, value){
this.allnotes.insertAdjacentHTML('beforeend', this.constructor.userNoteTemplate(key, value))
this.totalNotes();
let note = document.querySelector(`#note-${key}`);
note.addEventListener("click", this.deleteNote.bind(this));
}
deleteNote(){
let instance = this;
let element = ?// I want the element details also
}
答案 0 :(得分:1)
事件处理程序传递event object,可让您访问各种信息,其中包括the element the handler is bound to:
deleteNote(event){
let instance = this;
let element = event.currentTarget;
}