javascript--event compatibility (partial)

Source: Internet
Author: User

<!--cross-browser event compatibility--><! DOCTYPE html>"Utf-8"> <body> <div> <a href="http://www.baidu.com"Id="Al">click me</a> </div> </body><script type="Text/javascript">varEventutil={addhindler:function (ele,type,func) {//Add Event         if(Ele.addeventlistener) {//In addition to IE8 and out of browser compatibleEle.addeventlistener (Type,func,false); }         Else if(ele.attachevent) {//IE8 and the following versions of the browserEle.attachevent (" on"+Type,func); }         Else{ele[" on"+type]=func}//compatible with all versions}, Removehindler:function (Ele,type,func) {//To Delete a binding event        if(Ele.removeeventlistener) {//In addition to IE8 and out of browser compatibleEle.removeeventlistener (Type,func,false); }        Else if(ele.dettachevent) {//IE8 and the following browsersele.dettachevent[" on"+type]=func; }        Else{ele[" on"+type]=func;} }, Getevent:function (Event){//Get Events Event        return Event?Event: Window.Event;//compatible with IE for event}, Preventdefault:function (Event){//compatible with IE block event default behavior        Event. preventdefault?Event. Preventdefault ():Event. returnvalue=false; }, Gettarget:function (Event){//compatible with IE to get event targets        return Event. target| |Event. srcelement; }, Stoppropagation:function (Event){//compatible with IE stop event bubbling        Event. stoppropagation?Event. Stoppropagation ():Event. cancelbubble=true; }, Getrelatedtarget:function (Event){//MouseOver and mouseout have related element information, other mouse events return null        if(Event. Relatedtarget) {            return Event. Relatedtarget; }        Else if(Event. Fromelement) {            return Event. fromelement; }        Else if(Event. Toelement) {            return Event. toelement; }        Else            return NULL; }, Getbutton:function (Event){//Get the mouse button information, 0 for left, 1 for the scroll wheel, 2 for the right button        if(Document.implementation.hasFeature ("mouseevents","2.0")){            return Event. Button; }        Else{             Switch(Event. Button) {                 Case 0:                 Case 1:                 Case 3:                 Case 5:                 Case 7:                 return 0;//return the button with the left arrow (primary key) to the primary key                 Case 2:                 Case 6:                 return 2;//Assign a secondary key with a right key (secondary key) without a primary key                 Case 4:                 return 1//turn the rollers into rollers only             }        }    }, } varAl=document.getelementbyid ("Al"); function Fun (Event){     Event=eventutil.getevent (Event); Eventutil.stoppropagation (Event);//Stop event bubblingEventutil.preventdefault (Event);//block default behaviorEventutil.getrelatedtarget (Event); Eventutil.getbutton (Event); } Eventutil.addhindler (Al,"Click", fun)//To add a binding event</script>

javascript--event compatibility (partial)

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.