<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM事件对象</title>
</head>
<body>
<div>
<input type="button" value="按钮1" id="button1" onclick="showMes()" />
<input type="button" value="按键2" id="button2" />
<input type="button" value="按钮3" id="button3" />
</div>
<script type="text/javascript">
function showMes(){
event = event || window.event;
alert(event.type);
alert(event.target.nodeName);
}
var button2 = document.getElementById('button2');
var button3 = document.getElementById('button3');
button2.onclick = function(){
alert("DOM0级事件处理程序");
}
button2.onclick = null;
//下面处理兼容的问题,能力判断
var eventUtil = {
//添加事件
addHandler: function(element, type, handler){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}else if(element.attachEvent){
element.attachEvent('on'+type,handler);
}else{
//DOM0级,老版本浏览器
//element[onclick] === element.onclick
element['on'+type] = handler;
}
},
removeHandler: 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;
}
}
}
eventUtil.addHandler(button3,'click',showMes);
</script>
</body>
</html>
//IE中事件对象是作为全局对象( window.event )存在的,Firefox中则是做为句柄( handler )的第一个参数传入内的
//所以应该这样写
function showMes(){
var event = window.event||arguments[0];
alert(event.type);
var element=event.srcElement?event.srcElement:event.target;
alert(element.nodeName);
//由于IE不支持event.target属性,但是IE有event.srcElement,作用等同于event.target,所以可以这样写
}
图片的四种方法具体可以看http://wenku.baidu.com/view/400a89f4f61fb7360b4c65ca.html
window.onload=function(){}
没有加载