gpt4 book ai didi

javascript - 为什么按键事件需要一个参数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:45:50 27 4
gpt4 key购买 nike

为什么要加一个参数来处理事件?像这样:

  document.addEventListener( 'keydown', function( e ) {
var keyCode = e.keyCode } );

为什么你需要'e'?为什么函数根本不需要参数?

最佳答案

source :

通过作为第一个参数传递的事件对象,可以从处理函数中访问事件接口(interface)。

以下简单示例显示了如何将事件对象传递给事件处理函数,并可以在此类函数中使用。

function foo(evt) {
// the evt parameter is automatically assigned the event object
alert(evt);
}
table_el.onclick = foo;

source :

一个事件处理器H和一个事件对象E的事件处理器处理算法如下:

<强>1。让回调成为获取事件当前值的结果 处理程序 H.

<强>2。如果回调为空,则中止这些步骤。

<强>3。处理Event对象E如下:

If E is an ErrorEvent object and the event handler IDL attribute's type is OnErrorEventHandler

Invoke callback with five arguments, the first one having the value of E's message attribute, the second having the value of E's filename attribute, the third having the value of E's lineno attribute, the fourth having the value of E's colno attribute, the fifth having the value of E's error attribute, and with the callback this value set to E's currentTarget. Let return value be the callback's return value. [WEBIDL]

否则

Invoke callback with one argument, the value of which is the Event object E, with the callback this value set to E's currentTarget. Let return value be the callback's return value. [WEBIDL]

在这一步中,调用意味着运行跳转到代码入口点的算法。

<强>4。处理返回值如下:

If the event type is mouseover If the event type is error and E is an ErrorEvent object

If return value is a Web IDL boolean true value, then cancel the event. If the event type is beforeunload

The event handler IDL attribute's type is OnBeforeUnloadEventHandler, and the return value will therefore have been coerced into either the value null or a DOMString.

If the return value is null, then cancel the event.

否则

If the Event object E is a BeforeUnloadEvent object, and the Event object E's returnValue attribute's value is the empty string, then set the returnValue attribute's value to return value.

否则

如果返回值为 Web IDL bool 假值,则取消事件。

关于javascript - 为什么按键事件需要一个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644507/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com