Js dynamically assigns values to the onclick event of the object, and dynamically transmits parameters to give two examples: one-to-one error. If you are interested, you can compare them to find the differences.
Let's take a look at the error example first.
Html code
The Code is as follows:
Javascript code
The Code is as follows:
Script
Function show (value)
{
Alert (value );
}
Btn. onclick = show (certid. value );
Script
The above code is incorrectly executed because the show (certid. value) method is executed directly, but the method object is not correctly assigned to the btn. onclick event.
If this is the case
The Code is as follows:
Btn. onclick = show;
The parameter cannot be passed.
Therefore, the correct code should be written in this way. We can add a parameter to better understand it:
Html code
The Code is as follows:
Javascript code
The Code is as follows:
Script
Function show (value1, value2)
{
Alert (value1 + "," + value2 );
}
Var I = 10;
Btn. onclick = function (){
Show (certid. value, I );
};
Script
In this way, the onclick event handle value is dynamically assigned, and parameter transmission is supported.