如何在事件处理程序中访问对象和元素

时间:2017-04-26 16:09:40

标签: javascript dom event-handling dom-events

有没有办法将元素和实例传递给事件处理程序回调?

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
    }

1 个答案:

答案 0 :(得分:1)

事件处理程序传递event object,可让您访问各种信息,其中包括the element the handler is bound to

deleteNote(event){
    let instance = this;
    let element = event.currentTarget;
}