js 事件相關
1.綁定
function(element, type, handler){
if (element.addEventListener) {
element.addEventListener(type, handler, false);
}
else
if (element.attachEvent) {
element.attachEvent("on" + type, handler);
}
else {
element["on" + type] = handler;
}
}
2.刪除
function(element, type, handler){
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
}
else
if (element.detachEvent) {
element.detachEvent("on" + type, handler);
}
else {
element["on" + type] = null;
}
}
3.阻止默認事件
function(event){
if (event.preventDefault) {
event.preventDefault();
}
else {
event.returnValue = false;
}
}
4.不傳播事件(阻止冒泡)
function(event){
if (event.stopPropagation) {
event.stopPropagation();
}
else {
event.cancelBubble = true;
}
}
5.獲得event的target
function(event){
return event.target || event.srcElement;
}
js 事件相關