2 linonlooker Linonlooker 于 2016.04.26 16:14 提问

关于load问题,以下代码运行后为什么没有弹出
 <!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>

2个回答

showbo
showbo   Ds   Rxr 2016.04.26 17:12
已采纳

。。。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;
        }
Linonlooker
Linonlooker 呃..脑抽了 解决了已经
一年多之前 回复
ST091ZSC
ST091ZSC   2016.04.26 16:41

addHandler: function (element, type, handler) {
},
改为
addHandler: function (element, type, handler) {
handler();
},

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!