事件绑定函数的demo如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8"/>
<title>测试事件绑定函数</title>
<script>
window.onload = function(){
var liObj, ulObj;
ulObj = document.getElementsByTagName('ul')[0];
addEvent(ulObj, 'click', clickObjLi)
};
function addEvent(ele, event_name, func){
if(window.attachEvent){
ele.attachEvent(event_name, func);
}
else{
event_name = event_name.replace(/^on/, "");
ele.addEventListener(event_name, func, false);
}
}
function clickObjLi (event){
var currTarget = event.target;
alert(currTarget.innerHTML);
}
</script>
</head>
<body>
<div>
<ul>
<li class="item1">测试事件绑定1</li>
<li class="item2">测试事件绑定2</li>
<li class="item3">测试事件绑定3</li>
<li class="item4">测试事件绑定4</li>
</ul>
</div>
</body>
</html>