How do you catch a click event with plain Javascript?

后端 未结 6 1616
挽巷
挽巷 2021-01-31 10:11

I know that when using jQuery you can do $(\'element\').click(); to catch the click event of an HTML element, but how do you do that with plain Javascript?

6条回答
  •  心在旅途
    2021-01-31 10:16

    Thanks to vanilla js

    document.addEventListener('click', function (event) {
    
    // If the clicked element doesn't have the right selector, bail
    if (!event.target.matches('input')) return;
    
    // Don't follow the link
    event.preventDefault();
    
    // Log the clicked element in the console
    console.log(event.target);
    
    }, false);
    

    This also show which element is c

提交回复
热议问题