在 Javascript/jQuery 中 (e) 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10323392/
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
In Javascript/jQuery what does (e) mean?
提问by shrewdbeans
I am new to JavaScript/jQuery and I've been learning how to make functions. A lot of functions have cropped up with (e) in brackets. Let me show you what I mean:
我是 JavaScript/jQuery 的新手,我一直在学习如何创建函数。许多函数都出现在括号中的 (e) 中。让我告诉你我的意思:
$(this).click(function(e) {
// does something
});
It always appears that the function doesn't even use the value of (e), so why is it there so often?
该函数似乎总是不使用 (e) 的值,那么为什么它经常出现呢?
回答by Selvakumar Arumugam
e
is the short var reference for event
object which will be passed to event handlers.
e
是event
将传递给事件处理程序的对象的简短 var 引用。
The event object essentially has lot of interesting methods and properties that can be used in the event handlers.
事件对象本质上有很多有趣的方法和属性,可以在事件处理程序中使用。
In the example you have posted is a click handler which is a MouseEvent
在您发布的示例中是一个点击处理程序,它是一个 MouseEvent
$(<element selector>).click(function(e) {
// does something
alert(e.type); //will return you click
}
DEMO- Mouse Events DEMO uses e.which
and e.type
DEMO- 鼠标事件 DEMO 使用e.which
和e.type
Some useful references:
一些有用的参考资料:
http://api.jquery.com/category/events/
http://api.jquery.com/category/events/
http://www.quirksmode.org/js/events_properties.html
http://www.quirksmode.org/js/events_properties.html
http://www.javascriptkit.com/jsref/event.shtml
http://www.javascriptkit.com/jsref/event.shtml
http://www.quirksmode.org/dom/events/index.html
http://www.quirksmode.org/dom/events/index.html
回答by dsanchez
DISCLAIMER:This is a very late response to this particular post but as I've been reading through various responses to this question, it struck me that most of the answers use terminology that can only be understood by experienced coders. This answer is an attempt to address the original question with a novice audience in mind.
免责声明:这是对这个特定帖子的一个很晚的回复,但是当我阅读了对这个问题的各种回复时,让我震惊的是,大多数答案使用的术语只有有经验的程序员才能理解。这个答案是为了解决新手观众的原始问题。
Intro
介绍
The little '(e)' thing is actually part of broader scope of something in Javascript called an event handling function. Every event handling function receives an event object. For the purpose of this discussion, think of an object as a "thing" that holds a bunch of properties (variables) and methods (functions), much like objects in other languages. The handle, the 'e' inside the little (e)thing, is like a variable that allows you to interact with the object (and I use the term variableVERY loosely).
' (e)'这个小东西实际上是 Javascript 中称为事件处理函数的更广泛范围的一部分。每个事件处理函数接收一个事件对象。出于本次讨论的目的,可以将对象视为拥有一堆属性(变量)和方法(函数)的“事物”,就像其他语言中的对象一样。句柄,小(e)东西中的“ e” ,就像一个变量,允许您与对象交互(我非常松散地使用术语变量)。
Consider the following jQuery examples:
考虑以下 jQuery 示例:
$("#someLink").on("click", function(e){ // My preferred method
e.preventDefault();
});
$("#someLink").click(function(e){ // Some use this method too
e.preventDefault();
});
Explanation
解释
- "#someLink" is your element selector (which HTML tag will trigger this).
- "click" is an event (when the selected element is clicked).
- "function(e)" is the event handling function (on event, object is created).
- "e" is the object handler (object is made accessible).
- "preventDefault()" is a method (function) provided by the object.
- “#someLink”是你的元素选择器(哪个 HTML 标签会触发这个)。
- “单击”是一个事件(单击所选元素时)。
- “function(e)”是事件处理函数(在事件上,对象被创建)。
- “e”是对象处理程序(使对象可访问)。
- “preventDefault()”是对象提供的方法(函数)。
What's happening?
When a user clicks on the element with the id "#someLink"(probably an anchor tag), call an anonymous function, "function(e)", and assign the resulting object to a handler, "e". Now take that handler and call one of its methods, "e.preventDefault()", which should prevent the browser from performing the default action for that element.
发生了什么?
当用户点击 id 为“#someLink”(可能是锚标记)的元素时,调用匿名函数“function(e)”,并将结果对象分配给处理程序“e”。现在使用该处理程序并调用其方法之一"e.preventDefault()",这将阻止浏览器对该元素执行默认操作。
Note:The handle can pretty much be named anything you want (i.e. 'function(billybob)'). The 'e' stands for 'event', which seems to be pretty standard for this type of function.
注意:句柄几乎可以命名为您想要的任何名称(即'function(billybob)')。“e”代表“事件”,这似乎是此类功能的标准。
Although 'e.preventDefault()' is probably the most common use of the event handler, the object itself contains many properties and methods that can be accessed via the event handler.
尽管“e.preventDefault()”可能是事件处理程序最常见的用途,但对象本身包含许多可以通过事件处理程序访问的属性和方法。
Some really good information on this topic can be found at jQuery's learning site, http://learn.jquery.com. Pay special attention to the Using jQuery Coreand Eventssections.
可以在 jQuery 的学习站点http://learn.jquery.com 上找到有关此主题的一些非常好的信息。请特别注意使用 jQuery 核心和事件部分。
回答by Peter Porfy
e
doesn't have any special meaning. It's just a convention to use e
as function parameter name when the parameter is event
.
e
没有什么特别的意义。这只是一个约定使用e
时,参数是函数的参数名称event
。
It can be
有可能
$(this).click(function(loremipsumdolorsitamet) {
// does something
}
as well.
以及。
回答by j08691
The e
argument is short for the event object. For example, you might want to create code for anchors that cancels the default action. To do this you would write something like:
该e
参数是短期的事件对象。例如,您可能希望为取消默认操作的锚点创建代码。为此,您将编写如下内容:
$('a').click(function(e) {
e.preventDefault();
}
This means when an <a>
tag is clicked, prevent the default action of the click event.
这意味着当一个<a>
标签被点击时,阻止点击事件的默认操作。
While you may see it often, it's not something you have to use within the function even though you have specified it as an argument.
虽然您可能经常看到它,但即使您已将其指定为参数,也不必在函数中使用它。
回答by James Johnson
In that example, e
is just a parameter for that function, but it's the event
object that gets passed in through it.
在那个例子中,e
只是该函数的一个参数,但它是event
通过它传入的对象。
回答by Gothburz
In jQuery e
short for event
, the current event object. It's usually passed as a parameter for the event function to be fired.
在 jQuerye
中event
,当前事件对象。它通常作为要触发的事件函数的参数传递。
Demo: jQuery Events
演示:jQuery 事件
In the demo I used e
在我使用的演示中 e
$("img").on("click dblclick mouseover mouseout",function(e){
$("h1").html("Event: " + e.type);
});
I may as well have used event
我也可能用过 event
$("img").on("click dblclick mouseover mouseout",function(event){
$("h1").html("Event: " + event.type);
});
Same thing!
一样!
Programmers are lazy we use a lot of shorthand, partly it decreases our work, partly is helps with readability. Understanding that will help you understand the mentality of writing code.
程序员很懒,我们使用了很多速记,部分是它减少了我们的工作,部分是有助于提高可读性。理解这一点将有助于你理解编写代码的心态。
回答by Jason Liu
Today I just wrote a post about "Why do we use the letters like “e” in e.preventDefault()?" and I think my answer will make some sense...
今天我刚刚写了一篇关于“为什么我们在 e.preventDefault() 中使用像“e”这样的字母?我认为我的回答会有意义......
At first,let us see the syntax of addEventListener
首先,让我们看看 addEventListener 的语法
Normally it will be: target.addEventListener(type, listener[, useCapture]);
通常它是: target.addEventListener(type, listener[, useCapture]);
And the definitionof the parameters of addEventlistener are:
而addEventlistener的参数定义为:
type:A string representing the event type to listen out for.
listener:The object which receives a notification (an object that implements the Event interface) when an event of the specified type occurs. This must be an object implementing the EventListener interface, or a JavaScript function.
type:表示要监听的事件类型的字符串。
listener:指定类型的事件发生时接收通知的对象(实现Event接口的对象)。这必须是实现 EventListener 接口的对象或 JavaScript 函数。
(From MDN)
(来自 MDN)
But I think there is one thing should be remarked:When you use Javascript function as the listener, the object that implements the Event interface(object event) will be automatically assigned to the "first parameter"of the function.So,if you use function(e) ,the object will be assigned to "e" because "e" is the only parameter of the function(definitly the first one !),then you can use e.preventDefault to prevent something....
但是我觉得有一点需要注意:当你使用Javascript函数作为监听器时,实现Event接口的对象(对象事件)会被自动分配给函数的“第一个参数”。所以,如果你使用function(e) ,该对象将被分配给“e”,因为“e”是该函数的唯一参数(绝对是第一个!),然后您可以使用 e.preventDefault 来防止某些事情....
let us try the example as below:
让我们试试下面的例子:
<p>Please click on the checkbox control.</p>
<form>
<label for="id-checkbox">Checkbox</label>
<input type="checkbox" id="id-checkbox"/>
</div>
</form>
<script>
document.querySelector("#id-checkbox").addEventListener("click", function(e,v){
//var e=3;
var v=5;
var t=e+v;
console.log(t);
e.preventDefault();
}, false);
</script>
the result will be : [object MouseEvent]5and you will prevent the click event.
结果将是:[object MouseEvent]5并且您将阻止单击事件。
but if you remove the comment sign like :
但如果你删除评论符号,如:
<script>
document.querySelector("#id-checkbox").addEventListener("click", function(e,v){
var e=3;
var v=5;
var t=e+v;
console.log(t);
e.preventDefault();
}, false);
</script>
you will get : 8and an error:"Uncaught TypeError: e.preventDefault is not a function at HTMLInputElement. (VM409:69)".
你会得到:8和一个错误:“Uncaught TypeError: e.preventDefault is not a function at HTMLInputElement. (VM409:69)”。
Certainly,the click event will not be prevented this time.Because the "e" was defined again in the function.
当然,这次点击事件不会被阻止。因为在函数中再次定义了“e”。
However,if you change the code to:
但是,如果您将代码更改为:
<script>
document.querySelector("#id-checkbox").addEventListener("click", function(e,v){
var e=3;
var v=5;
var t=e+v;
console.log(t);
event.preventDefault();
}, false);
</script>
every thing will work propertly again...you will get 8 and the click event be prevented...
一切都会再次正常工作......你会得到8 并且点击事件被阻止......
Therefore, "e" is just a parameter of your function and you need an "e" in you function() to receive the "event object" then perform e.preventDefault(). This is also the reason why you can change the "e" to any words that is not reserved by js.
因此,“e”只是您函数的一个参数,您需要在 function() 中使用“e”来接收“事件对象”,然后执行 e.preventDefault()。这也是为什么你可以把“e”改成任何js没有保留的词的原因。
回答by keune
It's a reference to the current event object
它是对当前事件对象的引用
回答by kavya
$(this).click(function(e) {
// does something
});
In reference to the above code$(this)
is the element which as some variable.click
is the event that needs to be performed.
the parameter e
is automatically passed from js to your function which holds the value of $(this)
value and can be used further in your code to do some operation.
参考上面的代码$(this)
是作为一些变量的元素。click
是需要执行的事件。
该参数e
会自动从 js 传递到您的函数,该函数保存 value 的$(this)
值,并可在您的代码中进一步用于执行某些操作。