For example, you cannot use this invocation method: Element.onclick = Test (); Element.onclick = Test (ARG1,ARG2), only through Element.onclick = function () {...}; or Element.onclick = Test is implemented in this way, so arguments cannot be passed to the function. Refer to a large number of online data, the way to solve this problem, in code as an example, as follows:
Copy Code code as follows:
function Handler () {};
Handler.prototype = {
/*
* Bind EventType types of events to element elements and handle them with handler event handles
* Compatible browsers such as IE and Firefox
*
* @param the object on which the element registers the event (object)
* @param eventtype Registered event type (String) without "on"
* @param handler event handle (Function)
*/
Registerevent:function (element, EventType, handler) {
Event handling for the IF (element.attachevent) {//Level 2 DOM
Element.attachevent (' on ' + EventType, handler);
}else if (element.addeventlistener) {
Element.addeventlistener (EventType, Handler, false);
Event handling of the else {//Level 0 DOM
element[' on ' + eventtype] = handler;
}
},
/*
* Get a reference to an event handle with parameters
*
* @param obj requires the owner of the binding event handler function, null represents the Window object
* @param func The number of event-handling functions that need to be bound
* @param ... The third argument begins as a parameter to the binding event handler function, consisting of 0 to many
*/
Bind:function (obj, handler) {
obj = obj | | Window
var args = [];
for (var i =2 < arguments.length; i++)
{
Args.push (Arguments[i]);
}
return function () {handler.apply (obj, args)};
}
}
May be used in the following ways:
Function Show (txtobj) {
alert (Txtobj.value);
Txtobj.focus ();
Txtobj.select ();
}
Window.onload = function () {
var handler = new Handler ();
Handler.registerevent ($ ("txt"), "Change", Handler.bind (null,show,$ ("TXT")), and/or using the Level 2 event model
$ ("txt"). onchange = Handler.bind (null,show,$ ("TXT"));//javascript Event Properties
}