In the js nice things in general there are compatibility issues, the following, I compiled some commonly used treatment methods compatible, when their use can put them in a JS file, you need to use when directly introduced, it would be more convenient.
First, access to non-inline style
function getStyle(ele,attr){ if(ele.currentStyle){ return ele.currentStyle[attr]; }else{ return getComputedStyle(ele,false)[attr]; } }
[Note]: ARRT here as a variable to use bracket syntax.
Second, the acquisition of the event object
// Get event object compatible function getEvent (ELE) { var E = || ELE the window.event; }
Third, the event bubbling
// event bubbling compatible function stopBubble (E) { IF (e.stopPropagation) { e.stopPropagation (); } the else { e.cancelBubble = to true ; } }
Fourth, to prevent the browser's default behavior
// stop the default event compatible function Prevent (Eve) { var E = || Eve the window.event; IF (e.preventDefault) { e.preventDefault (); } the else { e.event.returnValue = to false ; } }
Tip: There is also a very simple wording, in the last line of code plus the return false; we must note that this code is written in the last line of a certain event, or not behind the code executes, when used pay attention.
Fifth, the event binding and delete events
1. Bind listening style event
//监听式绑定 function addEvent(ele,type,cb){ if(ele.addEventListener){ ele.addEventListener(type,cb); }else if(ele.attachEvent){ ele.attachEvent("on"+type,cb); }else { ele["on" + type] = cb; } }
2. Delete listens type of event
//删除绑定 function romoveEvent(ele,type,cb){ if(ele.removeEventListener){ ele.romoveEventListener(type,cb); }else if(ele.detachEvent){ ele.detachEvent("on"+type,cb); }else { ele["on"+type] = null; } }
Sixth, keyboard events
// keyboard compatible detection function KEYc (Eve) { var E = || Eve the window.event; var KEYc = e.keyCode || e.which; return KEYc; }