本文介绍了Javascript:什么是参数e(event),为什么传递给javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我学习javascript时,所有的书籍和互联网文章将参数e传递给处理javascript事件的函数,如下面的代码块:

Well when I learned javascript, all the books and internet articles passes a parameter e to a function that will handle javascript event, such as the code block below:

function myEvent(e) {
   var evtType = e.type
   alert(evtType)
   // displays click, or whatever the event type was
}

我一直接受它的方式,但现在我有一些问题这对我来说很困惑:

I've always accepted it the way it is, but now I have some questions about this and its very confusing to me:


  1. 这个e来自哪里?当我查看整个JavaScript文件时,e似乎根本不存在。

  2. 为什么把这个参数e传给函数?如果我不通过e,功能是否停止工作?

  3. 考虑下面的代码块,有一个事件变量(e)传递给匿名内部函数。假设我想使用匿名函数之外的事件对象(也许在element.onkeypress行之上/之下的一行),我该怎么做?

  1. Where does this e come from? When I look at the entire javascript file, e does not seem to exist at all.
  2. Why pass this parameter e to functions? Will the function stop working if I do not pass e to it?
  3. Consider the code block below, there is an event variable(e) passed to an anonymous inner function. Lets say I want to use event object outside of the anonymous function(maybe in a line above/below the element.onkeypress line), how can I do this?

element.onkeypress = function(e) {
                    if(e.keyCode) {
                        element.keyCode = e.keyCode;
                    } else {
                        element.keyCode = e.charCode;
                    }
                };



推荐答案

e 事件的缩写

最多创建事件的简单方法是单击页面上的某个位置。

The most simple way to create an event is to click somewhere on the page.

当您点击时,触发单击事件。这个事件实际上是一个包含刚发生的操作的信息的对象。在这个例子中,事件会有点击的坐标(例如, event.screenX ),你点击的元素( event.target )等等。

When you click, a click event is triggered. This event is actually an object containing information about the action that just happened. In this example's case, the event would have info such as the coordinates of the click (event.screenX for example), the element on which you clicked (event.target), and much more.

现在,事件一直发生,但是您对所有事件都不感兴趣发生。当您 对某些事件感兴趣时,那么当您将事件侦听器添加到您将知道将创建事件的元素时。例如,当您点击订阅按钮时,您有兴趣了解,并且您希望在此事件发生时执行

Now, events happen all the time, however you are not interested in all the events that happen. When you are interested in some event however, it's when you add an event listener to the element you know will create events. For example you are interested in knowing when the user clicks on a 'Subscribe' button and you want to do something when this event happens.

为了对这个事件做一些事情,你将一个事件处理程序绑定到你感兴趣的按钮中。将处理程序绑定到元素的方法是通过执行 element.addEventListener(eventName,handler)

In order to do something about this event you bind an event handler to the button you are interested in. The way to bind the handler to the element is by doing element.addEventListener(eventName, handler).

eventName 在这种情况下,您将感兴趣的事件的名称为'click'(对于单击事件) 。

eventName is the name of the event you are interested in, in this case that would be 'click' (for the click event).

处理程序只是一个函数,它会在事件发生时执行某些操作(它被执行)。默认情况下,处理函数接受当您感兴趣的事件/操作发生时创建的事件对象的参数。这就是你所提到的功能中的 e

The handler is simply a function which does something (it's executed) when the event happens. The handler function, by default, accepts as an argument the event object that was created when the event/action you are interested in happened. This is the e you see in the functions like the ones you mentioned.

定义事件作为您的处理函数的参数是可选的,但有时(大多数时候)它是有用的为处理函数了解发生的事件。请记住,事件只是一个常规的JavaScript对象,具有很多属性。

Defining the event as a parameter of your handler function is optional, however sometimes (most times) it is useful for the handler function to know about the event that happened. Remember, the event is just a regular javascript object, with lots of properties on it.

希望有所帮助。

有关详细信息,请参阅

For more info read Creating and Triggering Events

至于你的第三个问题,现在你应该知道你不能这样做,因为 e 仅在事件发生时才存在。您可以使用处理程序函数,该函数在执行时可以访问 e 对象,将其存储在一些全局变量中,并在此处执行。

As for your 3rd question, now you should know you cannot do that, because e only exists when an event happens. You could have the handler function, which has access to the e object when it gets executed, to store it in some global variable and work on that.

这篇关于Javascript:什么是参数e(event),为什么传递给javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:45
查看更多