1. all the previously written functions have been scattered. If you use any function, you can write any function that is not neat enough. Therefore, this write is an encapsulated class, which is not bad to use, however, many problems have been encountered when passing parameters. Therefore, I have read a lot of information and summarized it as follows:
1) dynamic binding event:
You need to bind the onclick event to an object, such as a list item. You need to use addEventListener or attachEvent to add the function operation to the event instead of overwriting. However, attachEvent does not support FF, and FF can only use addEventListener. So, we need a function to combine them. Thus, this function was born:
Function addEventHandler (oTarget, sEventType, fnHandler)
{
If (oTarget. addEventListener)
{OTarget. addEventListener (sEventType, fnHandler, false );}
Else if (oTarget. attachEvent)
{OTarget. attachEvent ('on' + sEventType, fnHandler );}
Else {oTarget ['on' + sEventType] = fnHandler ;}
}
2) question about passing this parameter:
Because my functions and attributes are encapsulated in a class, events such as onclick binding may cause a problem, for example, addEventHandler (this. elems [I], "click", this. move); in this case, an error occurs, because when the onclick event occurs, the called this does not point to the encapsulated class. Therefore, apply () is required ~ -- Use a method of an object to replace the current object with another object. I don't need to mention the specific format. There are a lot of online content ~ Function:
Var Bind = function (object, func ){
Var args = Array. prototype. slice. call (arguments). slice (2 );
Return function (){
Return func. apply (object, args );
}
}
Call:
This. _ fnMove = Bind (this, this. move, I); // this. move is a member function defined by me and encapsulated in the class.
// This. elems [I]. onclick = this. _ fnMove; // you can replace the above sentence with this sentence, but the onclick event is replaced with this. _ fnMove, instead of adding this. _ fnMove in
AddEventHandler (this. elems [I], "click", this. _ fnMove );
So OK ~
PS. call () is also basically the same function, but the specific parameters are different
- 2 pages in total:
- Previous Page
- 1
- 2
- Next Page