For different browsers, the event response will be different, so we have to consider good event compatibility issues for better user results.
In order to be compatible with different browsers, we can write an event object by itself, add functions and delete functions to add/remove objects to the element through its events.
We add an object to JS
varEventfunc={//defining an Event Add functionaddevent:function(object,eventname,funct) {//start writing Add-on events that are compatible with various browsers if(Object.addeventlistener) {Object.addeventlistener (eventname,funct,false);//DOM2 class }Else if(object.attachevent) {Object.attach ("On" +eventname,funct);//DOM2 level. And note that the event name is preceded by an "on" } Else{object["On" +eventname]=funct//Note that some of the lower versions of IE are not supported in either of the two ways, so take the DOM0 level, and note that here a place where the event name is preceded by "on"Then some people would say object."On" +eventname=funct not? That's not going to work, "." The following is not a string, so we can do it by [] instead of "."This place must be noticed. } //Defining the Delete event function if(Object.addeventlistener) {Object.removeeventlistener (eventname,funct,false);//DOM2 class }Else if(object.attachevent) {Object.detach ("On" +eventname,funct);//DOM2 level. And note that the event name is preceded by an "on" } Else{object["On" +eventname]=NULL//Note that some of the lower versions of IE are not supported in either of the two ways, so take the DOM0 level, and note that here a place where the event name is preceded by "on"Then some people would say object."On" +eventname=funct not? That's not going to work, "." The following is not a string, so we can do it by [] instead of "."This place must be noticed. } }
If we want to add a click event to an element, it will be all right.
Eventfunc.addevent (Object, "click", Function () {...}) Where we are "click", not "onclick", and the function name can be written directly later: for example, if there is a function showmes () {...}
Then we can directly write showmes, note, here is not showmes (), nor "Showmes",
JS Event 2-Event compatibility issues