jQuery 理解 JavaScript originalEvent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19617902/
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-08-27 00:18:19  来源:igfitidea点击:

Understanding JavaScript originalEvent

javascriptjquery

提问by bobbyjones

Can someone help me understand the use of originalEventin JavaScript? I really can't find a good source of documentation about it.

有人可以帮助我理解originalEvent在 JavaScript 中的使用吗?我真的找不到关于它的良好文档来源。

Google results led me to discussion sites that were too complicated to understand for a newbie.

谷歌搜索结果让我进入了对新手来说太复杂而无法理解的讨论网站。

I recently had a question in SO, and a guy answered it by adding this line of code

我最近在 SO 中有一个问题,一个人通过添加这行代码来回答它

$("#url").bind('paste', function(e) {
    var val = e.originalEvent.clipboardData.getData('text/plain');
 ....

to my existing code, which worked btw.

到我现有的代码,顺便说一句。

I would greatly appreciate if someone can help me understand the use of it.

如果有人能帮助我理解它的使用,我将不胜感激。

回答by joews

You are using a JavaScript library called jQuery, which is where the $() function comes from. jQuery wraps several parts of JavaScript to make it easier to use. One of those parts is event handling. In your example, because you're using jQuery to bind to the paste event, the object passed to your callback (e) is a jQuery event object, not a built-in JavaScript event object. The jQuery event object exposes the originalEventproperty to give you access to the underlying built-in event object.

您正在使用一个名为 jQuery 的 JavaScript 库,它是 $() 函数的来源。jQuery 包装了 JavaScript 的几个部分以使其更易于使用。其中之一是事件处理。在您的示例中,由于您使用 jQuery 绑定到 paste 事件,因此传递给回调 (e) 的对象jQuery 事件对象,而不是内置的 JavaScript 事件对象。jQuery 事件对象公开该originalEvent属性,让您可以访问底层的内置事件对象。

In your example you need to get hold of the clipboard data, which isn't available through the jQuery event object so you need to access the original event object to get at it.

在您的示例中,您需要获取剪贴板数据,该数据无法通过 jQuery 事件对象获得,因此您需要访问原始事件对象以获取它。

回答by kol

Certain events may have properties specific to them. Those can be accessed as properties of the event.originalEvent object.

某些事件可能具有特定于它们的属性。这些可以作为 event.originalEvent 对象的属性访问。

Source: jQuery Event object

来源:jQuery 事件对象

In your example, the clipboardDataproperty of the pasteeventis accessed via event.originalEvent.

在您的示例中,事件clipboardData属性是通过.pasteevent.originalEvent

回答by thecodeHyman

Well...originalEventis not directly from javascript as per my knowledge. it is the one that is triggered by browser. Jquery wraps up some more properties and the original event from browser is wrapped in originalEvent. Here is what i found from jquery site.

嗯……originalEvent据我所知,不是直接来自 javascript。它是由浏览器触发的。Jquery 包装了更多的属性,来自浏览器的原始事件包装在originalEvent. 这是我从 jquery 站点找到的内容。

"It's also important to note that the event object contains a property called originalEvent, which is the event object that the browser itself created. jQuery wraps this native event object with some useful methods and properties, but in some instances, you'll need to access the original event via event.originalEvent for instance. This is especially useful for touch events on mobile devices and tablets."

“同样重要的是要注意事件对象包含一个名为 originalEvent 的属性,它是浏览器本身创建的事件对象。jQuery 用一些有用的方法和属性包装了这个原生事件对象,但在某些情况下,你需要例如,通过 event.originalEvent 访问原始事件。这对于移动设备和平板电脑上的触摸事件特别有用。”