js中的兼容
1、事件对象的兼容解决:
var e = eve || window.event;
2、键盘事件的兼容解决:
var code = e.keyCode || e.which;
3、事件源的兼容解决:
var t = e.target || e.srcElement;
4、阻止默认事件的兼容解决:
1 function stopDefault(eve) {
2 if(eve.preventDefault) {
3 eve.preventDefault();
4 }else {
5 eve.returnValue = false;
6 }
7 }
8 //解释:eve,事件对象
5、监听式绑定事件的兼容解决:
1 function addEvent(ele,type,fn) {
2 if(ele.addEventListener){
3 ele.addEventListener(type,fn);
4 }else if(eve.attachEvent){
5 ele.attachEvent("on"+type,fn);
6 } else {
7 ele["on"+type] = fn;
8 }
9 }
6、监听式删除事件的兼容解决:
1 function removeEvent(ele, type, fn) {
2 if (ele.removeEventListener) {
3 ele.removeEventListener(type, fn);
4 } else if (eve.detachEvent) {
5 ele.detachEvent("on" + type, fn);
6 } else {
7 ele["on" + type] = fn;
8 }
9 }
7、事件冒泡的兼容解决:
1 function stopBubble(e) {
2 if (e.stopPropagation) {
3 e.stopPropagation();
4 } else {
5 e.cancelBubble = true;
6 }
7 }
8、获取非行内样式的兼容解决:
1 function getStyle(obj, attr) {
2 if (obj.currentStyle) {
3 return obj.currentStyle[attr];
4 } else {
5 return getComputedStyle(obj, false)[attr];
6 }
7 }