How to access the events properly when using jQuery.extend()
I am adding events to an extended object in jQuery using jQuery.extend();
Using jQuery 1.4.4, I was accessing an attribute named __events__
and I could access this in a predictable manner in order to add my own event hooks.
Upon inspecting jQuery 1.5.1 objects, I noticed 开发者_如何转开发the creation of a random attribute on the object upon using the extend function.
The attribute has this format: jquery151******************
(where * are numbers randomly generated).
Now I am blocked because I need to find a way to still access the same variable but because the name is random, I need a way to access it dynamically.
Does jQuery provides a way to do that? If not, what is the best way for me to add event hooks?
The numbers aren't as random as they appear, they're the expando property, it's accessible via jquery.expando
or $.expando
. The numbers are the timestamp (Date().getTime()
) from when the page was loaded, in an effort to not conflict with any other attributes.
For example at the time of this answer, in this page:
$.expando === "jQuery151023621073900721967"
However, these are cleaner ways to do what you're doing, for example the events
collections is accessible via .data()
, like this:
var events = $("mySelector").data("events");
.data()
, .bind()
, etc all use this attribute internally, it's simply a key for where the element has data in the $.cache
object, the value of that attribute is the key in $.cache
. For example, to get all data, events, etc for an element, it'd look like this in the raw form:
var data = $.cache[element[$.expando]]
精彩评论