Listen for all events in JavaScript

后端 未结 6 1588
天涯浪人
天涯浪人 2020-11-28 13:35

I\'m trying to figure out how to listen for all events on a JavaScript object.

I know that I can add individual events with something like this

eleme         


        
6条回答
  •  孤街浪徒
    2020-11-28 14:00

    //listening for all click events on the document
       document.addEventListener('click', function (event) {
    
        //filtering for only events that happen on elements that contain the class
        //view_btn.          
      if (event.target.classList.contains( 'view_btn' )){
    //logging out the id of the element        
              var id_of_clicked_element = event.target.getAttribute("id"); //
              console.log("button clicked has is of " + id_of_clicked_element)
    
            }
        });
    

提交回复
热议问题