<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加事件函数封装</title>
<style>
div{
height: 100px;
100px;
background: red;
}
</style>
</head>
<body>
<div id="div"></div>
<script>
function addListener(target,type,handler) {
if(target.addEventListener) {
target.addEventListener(type,handler,false)
}else if(target.attachEvent) {
target.attachEvent("on" + type,handler)
}else{
target["on" + type] = handler
}
}
var oDiv = document.getElementById("div");
function changeStyle() {
this.style.backgroundColor = 'blue';
}
addListener(oDiv,'click',changeStyle)
</script>
</body>
</html>
关于事件绑定的函数封装有很多,只是做一个小的汇总!