我正在尝试编写一个跨浏览器事件添加方法,但它在 IE 中根本不起作用,这是代码:
索引.html
<!doctype html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="helper.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
助手.js
var eventUtil = {
add : function(el, type, fn){
if(typeof(addEventListener) !== "undefined"){
el.addEventListener(type, fn, false);
} else if (typeof(attachEvent) !== "undefined"){
el.attachEvent("on"+type, fn);
} else {
el["on"+type] = fn;
}
}
}
脚本.js
(function(){
var printMsg= function(){
document.write("hello");
};
eventUtil.add(document.body, "click" , printMsg);
}());