问答详情
源自:2-4 IE事件处理程序及跨浏览器解决

运行不了,单词都核对过了

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="box">
    <input type="button" value="按钮" id="btn">
</div>
<script>
    var btn=getElementById("btn"); //获取id
    function showMes() { //执行的函数
        alert("hello world!");
    }
    var eventUtil={
        //添加句柄
        addHandler:function(element,type,handler){
            if(element.attachEvent){
                element.attachEvent('on'+type,handler);
            }else if(element.addEventListener){
                element.addEventListener(type,handler,false);
            }else{
                element['on'+type]=handler;
            }
        },
        //删除句柄
        removeHandler:function(element,type,handler){
            if(element.detachEvent){
                element.detachEvent('on'+type,handler);
            }else if(element.removeEventListener){
                element.removeEventListener(type,handler,false);
            }else{
                element['on'+type]=null;
            }
        }
    }
    eventUtil.addHandler(btn,'click',showMes); //执行添加句柄函数
    //eventUtil.removeHandler(btn,'click',showMes); //执行删除句柄函数
</script>
</body>
</html>


提问者:KevinLL 2016-07-27 22:10

个回答

  • KevinLL
    2016-07-27 22:17:37

    哦,找到原因了。

    var btn=getElementById("btn"); //获取id 

    没加document