是否可以为 jQuery 事件对象添加自定义属性?
示例代码如下:
$('a').mousedown(function(event)
{
event.ABC = true;
});
$(window).mousedown(function(event)
{
console.log("event.ABC:",event.ABC);
//outputs "undefined "
if( event.ABC)
{
// do sth
}
else
{
//let it go
}
});
Sample code is as following:
$('a').mousedown(function(event)
{
event.ABC = true;
});
$(window).mousedown(function(event)
{
console.log("event.ABC:",event.ABC);
//outputs "undefined "
if( event.ABC)
{
// do sth
}
else
{
//let it go
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个
,然后你就可以像这样使用它
$("yourSelector").mousedown(function(e){
警报(e.ABC); //>> abcdef
});
示例
try this
and then you can use it like that
$("yourSelector").mousedown(function(e){
alert(e.ABC); // >> abcdef
});
exemple
其他人提到,每次回调都会重新创建 jQuery 事件,这就是为什么添加到 jQuery 事件的任何属性都会丢失。
但是,jQuery 事件有一个
originalEvent
属性,该属性确实在整个事件传播阶段持续存在。您可以只执行
event.originalEvent.ABC
,而不是event.ABC
。注意:如果您打算广泛使用此功能,您可能需要添加 myAppData 属性来存储所有数据,即
event.originalEvent.myAppData.ABC
。这样可以轻松识别哪些事件属性是您的自定义属性,并且还可以减少名称冲突的可能性。Others have mentioned that the jQuery event gets recreated for each callback, which is why any attribute you add to the jQuery event is lost.
However, the jQuery event has an
originalEvent
attribute which does persist through the entire event propagation phase.Instead of
event.ABC
, you can just doevent.originalEvent.ABC
.Note: If you intend on using this extensively, you may want to add a myAppData attribute to store all your data in, i.e.
event.originalEvent.myAppData.ABC
. This makes it easy to identify which event attributes are your custom ones and also reduces the possibility of name collisions.虽然我不确定你到底想做什么,但我猜如果你不必弄乱事件对象,事情会容易得多。只需在单击
时设置一个标志变量:
但您可以在 jQuery 中触发事件。就像这样:
Though I'm not sure exactly what you want to do, I'm guessing it would be a lot easier if you didn't have to mess with the events object. Just set up a flag variable when your
<a>
's are clicked:But you can trigger events in jQuery. Like so:
您的示例遇到的问题是事件对象是为每个回调重新创建的,它不是传递给每个事件回调函数的全局对象。
您最好的选择可能是在适当的范围级别创建您自己的 EventArgs 对象,并向其中添加您需要的所有属性。
The problem you are having with your example is the event object is created fresh for each callback, it's not a global object that gets passed around to every event callback function.
Your best bet is probably to create your own EventArgs object at whichever scope level is appropriate, and add all the properties to it that you need.
输出是未定义的,因为每当触发事件时就会创建新的事件对象。您刚刚修改了一些不同的事件对象。不幸的是,除了重写 jQuery 之外,没有办法做到这一点。 ;) 尝试使用其他一些(全局)变量/对象。
The output is undefined, because whenever the event is triggered then new event object is created. You just modified some different event object. And unfortunetly there is no way to do this except for rewriting jQuery. ;) Try using some other (global) variables/objects.
您可以向事件对象添加附加数据。
然而,该数据仅在该事件处理程序中可用,而不是绑定到窗口的数据。
You can add additional data to the event object.
However that data will only be available inside that event handler, not the one bound to the window.