<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="#" id="link1" aaa="a"> test link</a>
<input type="button" value="attachEvent" onclick="addEvent();" />
</div>
</form>
<input type="text" id="msg" value="111"/>
</body>
</html>
<script type="text/javascript">
function bind(obj, type, fn, argsObject) {
var eventHandler = fn;
if(argsObject){
eventHandler = function(e){
fn.call(argsObject, e);
};
}
if(window.attachEvent){//IE
obj.attachEvent("on" + type, eventHandler );
}else{//FF
obj.addEventListener(type, eventHandler, false);
}
};
function unBind(obj, type, fn) {
if (obj.attachEvent) {
obj['e' + type + fn] = fn;
obj[type + fn] = function(){obj['e' + type + fn](window.event);};
obj.detachEvent("on"+type,fn);
} else {
obj.removeEventListener(type,fn);
};
};
function linkClick(e) {
/* this:是bind时的最后一个参数 e.target:是触发事件的对象 */
this.value = "333";
var target = e.srcElement || e.target;
alert(target.getAttribute('aaa'));
}
function addEvent() {
var msg = document.getElementById("msg");
var link1 = document.getElementById("link1");
bind(link1, "click", linkClick, msg);
}
</script>