简体   繁体   English

点击事件监听器

[英]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. 我正在开发基于Electron的应用程序,但对它或JavaScript或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. 据我了解,addEventListener中的第一个参数只是任意字符串。 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? 这是JavaScript,Node.js或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. JavaScript具有addEventListener函数,该函数将事件侦听器(惊讶,意外)添加到元素。 The element in which the listener is applied to now listens for an event, a string passed into the function (in this case click). 现在,在其中应用了侦听器的元素侦听一个事件,该事件是一个传递给函数的字符串(在本例中为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. 每次单击element都会打个hello

You can read more at the Mozilla's Documentation . 您可以在Mozilla的文档中阅读更多内容。 Here's a list of all the available events. 是所有可用事件的列表。

The first argument is string which represent the event type . 第一个参数是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 其中Event是事件对象,而click已经是javascript的预定义事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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