<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p id="p1">测试添加事件:firefox使用addEventListener,ie使用attachEvent<br>
点击此p标签,绑定了2个弹出事件</p>
<script>
function test1() {
alert("test1");
}
function test2(){
alert("test2");
}
//添加事件通用方法
function addEvent(element,e,fn) {
//firefox使用addEventListener,来添加事件
if(element.addEventListener) {
element.addEventListener(e,fn,false);
}
//ie使用attachEvent,来添加事件
else {
element.attachEvent("on"+e,fn);
}
}
window.onload = function(){
var element = document.getElementById("p1");
addEvent(element,"click",test1);
addEvent(element,"click",test2);
}
</script>
</body>
</html>