The inline onclick code is as follows :
Copy Code code as follows:
<input type= "button" id= "Btnok" name= "" value= "to determine" onclick= "Btnokclick ();"/>
Btnokclick's Code:
Copy Code code as follows:
function Btnokclick () {
Alert ("Btnok clicked");
}
Now, after clicking the button, remove the onclick event and bind a new click event for the button. At the second click, the second event handler is executed, and the second handler is the code for the function:
Reclick's Code:
Copy Code code as follows:
function Reclick () {
Alert (' Reclick ');
}
[\s\s]*\n
train of Thought: Remove the onclick in Btnokclick and add a new binding with the following code:
Copy Code code as follows:
$ (' #btnOK '). attr (' onclick ', '). Bind (' click ', Function () {Reclick ();});
The Btnokclick method after adding this code is as follows:
Copy Code code as follows:
function Btnokclick () {
Alert ("Btnok clicked");
$ (' #btnOK '). attr (' onclick ', '). Bind (' click ', Function () {Reclick ();});
}
This method works well under Google Chrome, but in IE's compatibility mode it will call the Reclick method immediately, which is not the effect we want.
The reason for this effect seems to be that after the onclick execution, ie go back to see if there is a binding on the click of the handler, the structure is there, so immediately executed.
In order to solve this problem, we can change the idea, that is, delay binding click event. The specific code is as follows:
Copy Code code as follows:
function Btnokclick () {
Alert ("Btnok clicked");
settimeout (function () {
$ (' #btnOK '). attr (' onclick ', '). Bind (' click ', Function () {Reclick ();});
}, 1);
}
Here the settimeout timer is used, and after the timer is triggered, the OnClick property is removed and the code that binds click handler is executed.
After testing, in IE9 compatibility mode and incompatible mode can operate normally; Google Chrome also works.