Assume that the existing method checkname
// Pass without Parameters
Function checkname (){
Dosomething...
}
Now, add an onclick event to an element in the form through Js.
1. If no parameters are passed
You can directly write it like this:
OBJ. attachevent ("onclick", checkname );
The second parameter of attachevent is the name of the method to be called. No quotation marks are required, and no brackets are required after the method name,
In this way, obj. attachevent ("onclick", "checkname") or obj. attachevent ("onclick", checkname (); are all incorrect statements.
Alternatively, you can use the traditional method of adding events: obj. onclick = checkname;
2. If you want to pass Parameters
Because of the JS closure, parameters cannot be directly transmitted and must be called through intermediate functions.
OBJ. attachevent ("onclick", checkname2 (OBJ); function checkname (ARGs) {return function () {dosomethings ...}} or obj. attachevent ("onclick", function () {checkname2 (OBJ) ;}); // function checkname (ARGs) {dosomethings ...}