<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<div>
<input type="button" name="btn" id="btn" value="按钮" onclick="message()" />
<input type="button" name="btn" id="btn2" value="按钮2" />
<input type="button" name="btn" id="btn3" value="按钮3" />
</div>
<script type="text/javascript">
function message(){
alert('输出内容')};
//DOM0级事件处理
var btn2 = document.getElementById('btn2');
btn2.onclick = message;
//DOM2级事件处理
var btn3 = document.getElementById('btn3');
/*btn3.addEventListener('click',message,false);
//IE事件处理
btn3.attachEvent('onclick',message);*/
//跨浏览器
var handler= {
addhandler:function(element,type,handler){
if(element.addEventListener){
element.addEvertListener(type,handler,false);
}else if(element.attachEvent){
element.attachEvent('on'+type,handler);
}else{
element['on'+type]=handler;
}
}
}
handler.addhandler(btn3,'click',message);
</script>
</body>
</html>