In a recent project, there is a function point: there is a checkbox on the page that sends a JSONP request to the background when the user chooses or cancels the checkbox. The implementation was for this checkbox to add a onchange event, but the result is unexpected, for this reason, I studied in depth, found that the onchange event in IE and FF under the performance of the following problems.
problem ①: under FF, when the checkbox is selected, the onchange event is triggered immediately. However, when changing the checkbox's selected state in IE, it does not start onchange events immediately, but waits for the Checbox to lose focus before the event starts.
To solve this problem, I added the This.blur () statement to the checkbox's OnClick event because the onclick event was executed before the onchange event, adding This.blur in the onclick event () The checkbox loses focus and immediately departs onchange event. In this way, a second problem has been encountered.
problem ②: when the onclick event and This.blur are used at the same time, the error will be in IE.
Looked up some information on the net, finally discovered the Onpropertychange this event. This event is not triggered under FF. In IE, when the checkbox's selection state changes, it will start immediately. Thus, the final solution is obtained: under IE, the checkbox is bound to the Onpropertychange event, and under FF, the onchange event is bound to it.
The specific code implementation is as follows:
Copy Code code as follows:
var ua=navigator.useragent.tolowercase ();
var s=null;
var browser={
MSIE: (S=ua.match (/msie\s*) ([\d\.] +)/))? S[1]:false,
Firefox: (S=ua.match (/firefox\/) ([\d\.] +)/))? S[1]:false,
Chrome: (S=ua.match (/chrome\/) ([\d\.] +)/))? S[1]:false,
Opera: (S=ua.match (/opera). ( [\d\.] +)/))? S[1]:false,
Safari: (S=ua.match (/varsion\/) ([\d\.] +). *safari/)? s[1]:false
};
if (Browser.msie) {//If IE browser
Checkbox.onpropertychange=function () {
Do someting
}
}
else{
Checkbox.onchange=function () {
Do something
}
}