1.获取样式
function getStyle(obj,sName){
return (obj.currentStyle || getComputedStyle(obj,false))[sName]
}
2.获取元素
obj.previousElementSibling || obj.previousSibling
obj.nextElementSibling || obj.nextSibling
3.获取向上滚动的距离
document.body.scrollLeft || document.documentElement.scrollLeft
4.兼容事件对象
var oEvent = evt || event;
5.DOM事件的封装和兼容
// 事件的绑定
function addEvent (obj,type,fn){
if(obj.addEventListener){
obj.addEventListener(type,fn,false);
}else{
obj.attachEvent('on'+type , fn);
}
}
// 事件的移除
function removeEvent(obj,type,fn){
if(obj.removeEventListener){
obj.removeEventListener(type,fn,false);
}else{
obj.detachEvent('on'+type , fn);
}
}
6.阻止默认事件
oEvent.preventDefault && oEvent.preventDefault();
7.ready的封装兼容
function addReady (fn){
if(document.addEventListener){
document.addEventListener('DOMContentLoaded',fn,false);
}else{
document.attachEvent('onreadystatechange',function(){
if(document.readyState == 'complete'){
fn();
}
});
}
}
8.滚轮事件的封装兼容和使用
//使用
addWheel(obj,function(bDown){
if(bDown){
alert('向下')
}else{
alert('向上')
}
})
//封装
function addWheel(obj,fn){
//加事件
if(navigator.userAgent.toLowerCase().indexOf('Firefox') != -1){
obj.addEventListener('DOMMouseScroll',show,false)
}else{
obj.onmousewheel = show;
}
//执行事件
function show (ev){
var bDown = true;
// 确定到底是向上还向下,更改bDown的真假
var oEvent = ev || event;
if(oEvent.detail){
if(oEvent.detail>0){
bDown = true;
}else{
bDown = false;
}
}else{
if(oEvent.wheelDelta>0){
bDown = false;
}else{
bDown = true
}
}
fn(bDown);
}
}
9.事件委托兼容
var oLi = oEvent.srcElement || oEvent.target;