1. the following method is recommended by the father of jquery to add and remove events.
Java code
- /*
- * OBJ: the object to bind the event
- * Type indicates the event type, such as "click", "LOAD", "Submit", and "Mouseover ".
- * FN function name
- */
- Function addevent (OBJ, type, FN)
- {
If
(Obj. attachevent)
- {
- OBJ [
'E'
+ Type + FN] = FN;
- OBJ [type + FN] = function ()
- {
- OBJ [
'E'
+ Type + FN] (window. Event );
- }
- OBJ. attachevent (
'On'
+ Type, OBJ [type + FN]);
- }
Else
- OBJ. addeventlistener (type, FN,
False
);
- }
- /*
- * OBJ: the object of the event to be deleted.
- * Type indicates the event type, such as "click", "LOAD", "Submit", and "Mouseover ".
- * FN function name
- */
- Function removeevent (OBJ, type, FN)
- {
If
(Obj. detachevent)
- {
- OBJ. detachevent (
'On'
+ Type, OBJ [type + FN]);
- OBJ [type + FN] =
Null
;
- }
Else
- OBJ. removeeventlistener (type, FN,
False
);
- }
- // Addevent (document. getelementbyid ('foo'), 'click', dosomething );
2. according to "Pro JavaScript techniques", John resig greatly recommends the addevent () method of Dean Edwards, but does not recommend more streamlined works. He commented on Edwards's addevent:
1
It can work in all browsers, even if it is ancient and does not support any browsers;
- 2
,
This
Keywords can be used in all binding functions, pointing to the current element;
- 3
And all browser-specific functions that prevent default browser behavior and prevent event bubbles;
- 4
Regardless of the browser type, the event object is always passed in as the first object;
- 5
The only drawback is that it only works in the bubble stage (because the traditional method of event binding is used in depth ).
- 6
, The addevent/removeevent function is so powerful that there is no reason not to use it in your code.
1. It can work in all browsers, even if it is old and does not support any browsers; 2. This keyword can be used in all binding functions, pointing to the current element; 3, all browser-specific functions that prevent browser default behaviors and prevent event bubbles; 4. Regardless of the browser type, the event object is always passed in as the first object; 5, the only drawback is that it only works in the bubble stage (because the traditional method of event binding is used in depth ). 6. The addevent/removeevent function is so powerful that there is no reason not to use it in your code.
Dean Edwards said:
My solution is very different.
* It performs no object detection
* It does not use the addeventlistener/attachevent Methods
* It keeps the correct scope (the this keyword)
* It passes the event object correctly
* It is entirely cross-browser (it will probably work on ie4 and ns4)
* And from what I can tell it does not leak memory
The following is the addevent/removeevent function of Dean Edwards:
Java code
- // Http://dean.edwards.name/weblog/2005/10/add-event/
- Function addevent (element, type, Handler ){
If
(Element. addeventlistener ){
- Element. addeventlistener (type, handler,
False
);
- }
Else
{
// Assign an independent ID to each event processing function
If
(! Handler. $ guid) handler. $ guid = addevent. guid ++;
// Create an event-type hash table for the element
If
(! Element. Events) element. Events = {};
// Create an event handler hash table for each element/event
- VaR handlers = element. events [type];
If
(! Handlers ){
- Handlers = element. events [type] = {};
// Store existing event processing functions (if one already exists)
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 processing function to handle all tasks
- Element [
"On"
+ Type] = handleevent;
- }
- };
- // Create a counter with an independent ID
- 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
;
// Obtain the event processing object (ie uses the global event object)
- Event = event | fixevent (((
This
. Ownerdocument |
This
. Document |
This
). Parentwindow | window). Event );
// Obtain the reference of the hash table of the event processing function
- VaR handlers =
This
. Events [event. Type];
// Process each event processing function in sequence
For
(Var I in handlers ){
This
. $ Handleevent = handlers [I];
If
(
This
. $ Handleevent (event) ===
False
){
- Returnvalue =
False
;
- }
- }
Return
Returnvalue;
- };
- // Add some methods for the lack of IE event objects
- Function fixevent (event ){
// Added W3C standard event Methods
- Event. preventdefault = fixevent. preventdefault;
- Event. stoppropagation = fixevent. stoppropagation;
Return
Event;
- };
- Fixevent. preventdefault = function (){
This
. Returnvalue =
False
;
- };
- Fixevent. stoppropagation = function (){
This
. Cancelbubble =
True
;
- };