Window. addeventjs event-driven function set addEvent _ javascript skills
Source: Internet
Author: User
AddEvent (), removeEvent (), handleEvent (), fixEvent () [// written by Dean Edwards, 2005
// With input from Tino Zijdel, Matthias Miller, Diego Perini
Function addEvent (element, type, handler ){
If (element. addEventListener ){
Element. addEventListener (type, handler, false );
} Else {
// Assign each event handler a unique ID
If (! Handler. $ guid) handler. $ guid = addEvent. guid ++;
// Create a hash table of event types for the element
If (! Element. events) element. events = {};
// Create a hash table of event handlers for each element/event pair
Var handlers = element. events [type];
If (! Handlers ){
Handlers = element. events [type] = {};
// Store the existing event handler (if there is one)
If (element ["on" + type]) {
Handlers [0] = element ["on" + type];
}
}
// Store the event handler in the hash table
Handlers [handler. $ guid] = handler;
// Assign a global event handler to do all the work
Element ["on" + type] = handleEvent;
}
};
// A counter used to create unique IDs
AddEvent. guid = 1;
Function removeEvent (element, type, handler ){
If (element. removeEventListener ){
Element. removeEventListener (type, handler, false );
} Else {
// Delete the event handler from the hash table
If (element. events & element. events [type]) {
Delete element. events [type] [handler. $ guid];
}
}
};
Function handleEvent (event ){
Var returnValue = true;
// Grab the event object (IE uses a global event object)
Event = event | fixEvent (this. ownerDocument | this.doc ument | this). parentWindow | window). event );
// Get a reference to the hash table of event handlers
Var handlers = this. events [event. type];
// Execute each event handler
For (var I in handlers ){
This. $ handleEvent = handlers [I];
If (this. $ handleEvent (event) === false ){
ReturnValue = false;
}
}
Return returnValue;
};
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service