兼容方式
function addEvent(dom, type, fn) {
if (dom.addEventListener) {
dom.addEventListener(type, fn, false);
} else if (dom.attachEvent) {
dom.attchEvent('on' + type, fn);
} else {
dom['on' + type] = fn;
}
}
// 获取事件对象
var getEvent = function(event) {
return event || window.event;
}
// 获取元素
var getTarget = function() {
var event = getEvent(event);
return event.target || event.srcElement;
}
// 阻止默认行为
var preventDefault = function() {
var event = getEvent(event);
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
小型属性样式方法库
var A = {
g : function(id) {
return documen.getElementById(id);
},
css : function(id, key, value) {
return document.getELementById(id).style[key] = value;
},
attr : function(id, key, value) {
return document.getElementById(id)[key] = value;
},
html : function(id, html) {
return document.getElementById(id).innerHTML = html;
},
on : function(id, type, fn) {
return document.getElementById['on' + type] = fn;
}
};