简体   繁体   中英

Click event listener

I'm working on an Electron-based application, and I don't have much experience with it or JavaScript or Node.js. Currently, I just want to close a window by a click on a button.

close.addEventListener('click', function () {
    ipc.send('close-main-window')
})

This totally works! I am just confused with why it works. From what I understand, the first argument in addEventListener is just any arbitrary string. However, I don't specifically write anything to handle a 'click'. This should mean it's built in functionality, I think. Is this part of JavaScript, Node.js, or Electron? And where in the documentation can I find a list of built in events?

JavaScript has the function addEventListener which adds an event listener (surprise, surprise) to an element. The element in which the listener is applied to now listens for an event, a string passed into the function (in this case click). Once the event is triggered (in this case when a user clicks on the element), it will execute the callback, which is the function you declared. So, consider this:

element.addEventListener("click", function() {
    console.log("hello!");
});

This will log hello every time element is clicked.

You can read more at the Mozilla's Documentation . Here's a list of all the available events.

The first argument is string which represent the event type .

I think internally it works like this

var event = new Event('click');

where Event is an event object & click is already a predefined event of javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM