了解JavaScript originalEvent [英] Understanding JavaScript originalEvent

查看:789
本文介绍了了解JavaScript originalEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我了解JavaScript中originalEvent的用法吗?我真的找不到关于它的很好的文档来源.

Google的搜索结果使我进入了一个讨论站点,这些站点太过复杂以至于对于新手来说都不容易理解.

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

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

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

如果有人可以帮助我了解其用法,我将不胜感激.

解决方案

您正在使用一个名为jQuery的JavaScript库,该库是$()函数的来源. jQuery包装了JavaScript的多个部分,以使其更易于使用.这些部分之一是事件处理.在您的示例中,因为您使用的是jQuery绑定到粘贴事件,所以传递给回调(e)的对象是

to my existing code, which worked btw.

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

解决方案

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 originalEvent property to give you access to the underlying built-in event object.

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.

这篇关于了解JavaScript originalEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆