<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img id="myImage" src="http://www.baidu.com/img/2016_4_26logo_843a64cc86a54b8da14d7e9baad4d15f.gif"
alt="aa"/>
<script>
var EventUtil = {
addHandler: function (element, type, handler) {
},
getEvent: function (event) {
return event ? event : window.event;
},
getTarget: function (event) {
return event.target || event.srcElement;
},
//取消事件的默認行為
preventDefault: function (event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue =false; //IE
}
},
removeHandler: function (element, type, handler) {
},
//取消事件進一步冒泡
stopPropagation: function (event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
}
};
var image =document.getElementById("myImage");
EventUtil.addHandler(image, "load", function (event) {
event = EventUtil.getEvent(event);
alert(EventUtil.getTarget(event).src);
});
</script>
</body>
</html>
。。。addHandler代碼都沒寫你想怎麼樣。。
addHandler: function (element, type, handler) {
if (element.addEventListener) element.addEventListener(type, handler, false);
else if (element.attachEvent) element.attachEvent('on' + type, handler);
else element['on' + type] = handler;
}