javascript javaScript中函数内的evt参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29407968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:29:34  来源:igfitidea点击:

evt argument inside a function in javaScript

javascriptfunctionargumentsaddeventlistenerpredefined-variables

提问by Phil Rv

evt argument inside a function in javaScript

javaScript中函数内的evt参数

Hello stackoverflow community. I need to know how is the evtargument used in these functions. There are some examples in the internet which have the argument evt inside of a function, but I don't see them using the argument.

你好 stackoverflow 社区。我需要知道如何在这些函数中使用evt参数。互联网上有一些例子在函数内部有参数 evt ,但我没有看到它们使用参数。

document.getElementById("creator").addEventListener("click", function(evt){
    alert("created");
});

document.getElementById("change").addEventListener("click", function(evt){
    alert("changed");
});

I guess the evt argument is just set as undefined because those functions are never called with a value for the argument.

我猜 evt 参数只是设置为未定义,因为这些函数永远不会使用参数值调用。

  • So what is the reason for setting the argument variable evt?
  • can it be hello instead of evt?
  • if it is predefined to the event listeners, how can I find a list of predefined arguments?
  • 那么设置参数变量evt的原因是什么?
  • 它可以是 hello 而不是 evt 吗?
  • 如果它是为事件侦听器预定义的,我如何找到预定义参数的列表?

采纳答案by Richard Macarthy

When an event is invoked, it will be passed an event object as it's first argument. You can name evtwhatever you like. Common names are eevtand event.

当一个事件被调用时,它会被传递一个事件对象作为它的第一个参数。你可以随意命名evt。常用名称是eevtevent

I typically use this for things like

我通常将它用于诸如

event.preventDefault()Stop an events default action, on submit for example.

event.preventDefault()停止事件默认操作,例如提交。

and

event.targetFind the tagret of the element the event was invoked on.

event.target找到调用事件的元素的 tagret。

There are a lot more properties that can be used on the event object, and becomes very useful if you know how to use it. More information about event object here.

可以在事件对象上使用更多属性,如果您知道如何使用它,就会变得非常有用。关于事件对象的更多信息在这里。

https://developer.mozilla.org/en/docs/Web/API/Event

https://developer.mozilla.org/en/docs/Web/API/Event