// 属性的兼容,使用||解决
// 方法的兼容,使用if判断解决
- event事件获取兼容
//eve传入参数
var e = eve || window.event;
注意:event需要逐层传递,不要疏忽外部的function
- 非行内样式兼容
function getStyle(ele,attr){
if(ele.currentStyle){
return ele.currentStyle[attr]; //IE
}else{
return getComputedStyle(ele,false)[attr]; //非IE
}
}
alert(getStyle(obox,"background-color"));
console.log(getStyle(obox2,"background-color"));
- 获取按键ASCII码
var eve = eve || window.event
var keyC = eve.keyCode || eve.which
- 添加事件监听(IE兼容)
// 封装成函数
function addEvent(ele,type,callback){
if(ele.addEventListener){
ele.addEventListener(type,callback)
}else if(ele.attachEvent){
ele.attachEvent("on"+type,callback)
}else{
ele["on"+type] = callback;
}
}
function removeEvent(ele,type,callback){
if(ele.removeEventListener){
ele.removeEventListener(type,callback)
}else if(ele.detachEvent){
ele.detachEvent("on"+type,callback)
}else{
ele["on"+type] = null;
}
}
//removeEvent(obox,"click",fn1)
// 封装成对象
var myEvent = {
add:function(ele,type,callback){
if(ele.addEventListener){
ele.addEventListener(type,callback)
}else if(ele.attachEvent){
ele.attachEvent("on"+type,callback)
}else{
ele["on"+type] = callback;
}
},
remove:function(ele,type,callback){
if(ele.removeEventListener){
ele.removeEventListener(type,callback)
}else if(ele.detachEvent){
ele.detachEvent("on"+type,callback)
}else{
ele["on"+type] = null;
}
}
}
- 阻止事件冒泡兼容
function stopBubble(e){
if(e.stopPropagation){
e.stopPropagation()
}else{
e.cancelBubble = true;//IE
}
}
- 目标元素target兼容
target = e.target || e.srcElement;
- 阻止浏览器默认行为兼容
function(e){
if(e.preventDefault){
e.preventDefault();
}else{
window.event.returnValue = false;
}
}