Events compatibility problems

  

  Different browsers get the event source approach is different, which stems from the browser was designed with different browsers for different attributes define the event, let's take a look at a different browser compatibility issues for many of the same event .

 

1. Event Processing

  Event Source attribute different browsers is not the same in order to solve this problem, we are writing method, the writing method compatibility will get the event source.

Keyboard Events

fuction  keyDown(ev){
  var e = ev || window.event;
  var key = e.target || e.srcElement;        
  if(key==keyCode){
    //......
    }  
}

Mouse Events

Mouse function (EV) { 
    var EV E = || the window.event; 
    // Get event source coordinates, 
    var = e.target || Key e.srcElement 
    // or mouse button 
}

 

2. Event Listeners

When the object is bound to the event by the event listener method, due to the different methods monitor different browsers, will do some processing

funtion addEvent(ele,type,cb){
   return  ele.attachEvent?ele.attachEvent("on"+type,cb):ele.addEventListener(type,cb);
}

 Objects to remove the event there are compatibility issues

function removeEvent(ele,type,cb){
    return ele.detachEvent? ele.detachEvent(type,cb):ele.removeEvent("on"+type,cb);
}

3. event bubbling

Html in the box, it will penetrate the inside of the trigger event to the parent body, a phenomenon called event bubbling, in order to solve this problem, to stop the event bubbling

    stopBubble function (e) { 
        return window.stopPropagation? e.stopPropagation (): e.cancelBubble = true; 
    }

 In the IE browser is not event bubbling method, but property.

4. The event delegation

Event function obj.on + = (Eve) { 
        E = || the window.event var Eve; 
        var = e.target the tar || e.srcElement; 
        // determine the current event source, whether the conditions are met and other objects 
        // binding events 
    }

  

There are a lot of browser compatibility issues, because the browser kernel is not the same, different manufacturers, different browsers in the preparation of these designers thinking it is different, written in different languages. For programmers, less code to solve more problems, is the basic quality of our programmers necessary

Guess you like

Origin www.cnblogs.com/bigharbour/p/11934514.html