JS Event 2-Event compatibility issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.