日期:2014-05-16  浏览次数:20335 次

为什么DOM2级事件失效了???
为什么这一段代码执行不了呢? objEvent.addEvent(document,"mouseup",function(){
objEvent.addEvent(document,"mousemove",null);
//document.onmousemove = null; 这里把注释去掉也不行
})
如果使用注释的DOM0级的就可以..
这到底怎么回去啊 ?
难道通过这样objEvent.addEvent 的添加事件是无法做到事件等于NULL的吗?
HTML code
<!DOCTYPE HTML>
<html>
<head>
<title>soul42</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<STYLE type="text/css">
    * { margin:0;  padding:0;}
    #box{width:30px;margin:200px auto 0 ;display:block;position:relative;}
    .wrap{margin:0;width:200px;position:absolute;top:0;left:0;overflow:hidden; background:red}
    ul{position:absolute;top:0px;left:0;width:200px;}
    li{height:20px;background:orange;margin-bottom:0px}
    
</STYLE>

</head>




<body>
<DIV style="POSITION: absolute; WIDTH: 50px; BACKGROUND: #f00; HEIGHT: 50px; LEFT: 0px" id="mydiv"></DIV>  

    <script type="text/javascript">
        var $ = function(id){return document.getElementById(id)}
        var objEvent = {
            addEvent:function(elem,type,func){
                var _this = this;
                if(elem.addEventListener){
                    elem.addEventListener(type,func,false);
                }else if(elem.attachEvent){
                    elem.attachEvent("on"+type,func)
                }else{
                    elem["on"+type] = func
                }
            },
            getEvent:function(e){
                return e = e|| window.event; 
            },
            func:function(e){
                    e = objEvent.getEvent(e);
                    var posX = e.clientX - $("mydiv").offsetLeft;
                    (function(){
                        objEvent.addEvent(document,"mousemove",function(e){
                            e = objEvent.getEvent(e);
                            $("mydiv").style.left = (e.clientX - posX) +"px";
                        });
                        objEvent.addEvent(document,"mouseup",function(){
                            objEvent.addEvent(document,"mousemove",null);
                            //document.onmousemove = null;
                        })

                        /*document.onmousemove = function(e){
                            e = objEvent.getEvent(e);
                            $("mydiv").style.left = (e.clientX - posX) +"px";
                        }
                        document.onmouseup = function(e){
                            document.onmousemove = null;
                        }*/

                    })()
            }
        }
        objEvent.addEvent($("mydiv"),"mousedown",objEvent.func);
    </script>

</body>
</html>


------解决方案--------------------
objEvent.addEvent(document,"mousemove",null); 这个如果调用的是attachEvent
attachEvent 是 追加处理函数,并不影响原来的处理函数
----------
document.onmousemove = null; 这个问题等再查下.