【前端】EventUtil可以直接调用吗??
跨浏览器的这个方法,为啥直接调用显示 is not defined?
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title></title>
<script type="text/javascript">
var image = document.getElementById('img')
EventUtil.addHandle(img , "load" ,function(event){
event = EvenUtil.getEvent(event);
alert(EventUtil.getTarget(event).src)
});
</script>
</head>
<body>
<img id="img" src="https://segmentfault.com/q/1010000005361274/img/0.jpg"/>
</body>
</html>
回答
EventUtil你得自己封装
var EventUtil = { 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;
}
},
removeHandler: function (element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else if (element.detachEvent) {
element.detachEvent("on" + type, handler);
} else {
element["on" + type] = null;
}
},
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;
}
},
stopPropagation: function (event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubbles = true;
}
},
getRelatedTarget: function (event) {
if (event.relatedTarger) {
return event.relatedTarget;
} else if (event.toElement) {
return event.toElement;
} else if (event.fromElement) {
return event.fromElement;
} else { return null; }
}
}
这里有一篇对 EventUtil 对象非常详细的解释和用法描述,希望可以帮到你:http://www.cnblogs.com/hykun/...
另外,你是不是传入对象错了?
以上是 【前端】EventUtil可以直接调用吗?? 的全部内容, 来源链接: utcz.com/a/79420.html