JavaScript Event Properties binding a function with parameters _javascript tips

Source: Internet
Author: User
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
}
Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.