Bind event to element using pure Javascript

前端 未结 2 1360
一向
一向 2020-12-28 13:24

I\'m writing an API and unfortunately need to avoid any jQuery or 3rd party libraries. How exactly are the events bound to elements through jQuery?

jQuery example:

2条回答
  •  渐次进展
    2020-12-28 13:53

    Here's a quick answer:

    document.getElementById('anchor').addEventListener('click', function() {
      console.log('anchor');
    });
    

    Every modern browser supports an entire API for interacting with the DOM through JavaScript without having to modify your HTML. See here for a pretty good bird's eye view: http://overapi.com/javascript

提交回复
热议问题