admin管理员组文章数量:1287636
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
}
});
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
}
});
Share
Improve this question
edited Jun 10, 2021 at 9:32
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Oct 27, 2011 at 21:21
BoboBobo
9,16319 gold badges68 silver badges85 bronze badges
1
-
So clicking an
<a>
element will make clicking the window not doing anything anymore? Is that what you want? – pimvdb Commented Oct 27, 2011 at 21:25
6 Answers
Reset to default 6Others 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 do event.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.
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:
var eventABC = false;
$('a').mousedown(function() {
eventABC = true;
});
$(window).mousedown(function() {
console.log("eventABC:", eventABC);
//outputs true if <a> clicked, false otherwise
if(eventABC) {
// do sth
} else {
//let it go
}
eventABC = false;
});
But you can trigger events in jQuery. Like so:
$('a').click(function() {
$(window).trigger("eventABC", ['CustomProperty1', 'CustomProperty2']);
return false;
});
$(window).bind('eventABC', function(event, param1, param2) {
alert("eventABC triggered: " + param1);
});
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.
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.
$('a').bind('mousedown',{ABC:true},function(event)
alert(event.data.ABC);
});
However that data will only be available inside that event handler, not the one bound to the window.
try this
$("*").mousedown(function(e){
e.ABC = "abcdef";
}); //place this code first
and then you can use it like that
$("yourSelector").mousedown(function(e){
alert(e.ABC); // >> abcdef
});
exemple
本文标签: javascriptIs it possible to add a customized property for jQuery event objectStack Overflow
版权声明:本文标题:javascript - Is it possible to add a customized property for jQuery event object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741304693a2371297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论