Delpre:function(){
  
addEventHandler(this._obj, "click", BindAsEventListener(this, this.KeyDel));   //如何把delobj传给KeyDel函数呢?

},

  KeyDel:function(){

//alert(this._obj.id);
addEventHandler(document, "keydown", BindAsEventListener(this, this.Del));  //接下来需要销毁绑定以免重复执行


},

  Del:function(e){
  
  var keydel

if(window.event) // IE
   {
   keydel = e.keyCode
   }
else if(e.which) // Netscape/Firefox/Opera
   {
   keydel = e.which
   }
if(keydel==46){


deln[delnum]=this._obj.id;
alert(deln[delnum]); //被删除了的元素还会在这里alert
delnum++;
var mubiaoparent=this._obj.parentNode;
var removemubiao=mubiaoparent.removeChild(this._obj);

   

}  removeEventHandler(document, "keydown",  BindAsEventListener(this, this.Del));  //目前无效
 
  removeEventHandler(this._obj,"click", BindAsEventListener(this, this.KeyDel));
//目前无效
  
  },
如上的代码,调用了乘员函数Delpre()然后点击以及按delete键也可以把相关的元素删除,不过,删除之后貌似无法把绑定的事件销毁,导致下次再点击某元素按Delete键又会去删除已经被删除的东西,我认为是销毁有问题,不过不知道是为什么的问题,高手帮忙阿

解决方案 »

  1.   

    或者是unbind("事件",funciton(){});
      

  2.   

    大哥我还没有接触jquery,下面我上完整代码
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>拖拉缩放效果</title>
    </head>
     
    <body>
     
    <script>
    var deln=new Array();
    var delnum=0; 
     
    var isIE = (document.all) ? true : false;
     
    var $ = function (id) {
        return "string" == typeof id ? document.getElementById(id) : id;
    };
     
    var Class = {
        create: function() {
            return function() { this.initialize.apply(this, arguments); }
        }
    }
     
    var Extend = function(destination, source) {
        for (var property in source) {
            destination[property] = source[property];
        }
    }
     
    var Bind = function(object, fun) {
        return function() {
            return fun.apply(object, arguments);
        }
    }
     
    var BindAsEventListener = function(object, fun) {
        var args = Array.prototype.slice.call(arguments,2);
        //alert(args);
        return function(event) {
            return fun.apply(object, [event || window.event].concat(args));
        }
    }
     
    var CurrentStyle = function(element){
        return element.currentStyle || document.defaultView.getComputedStyle(element, null);

    function addEventHandler(oTarget, sEventType, fnHandler) {
        if (oTarget.addEventListener) {
            oTarget.addEventListener(sEventType, fnHandler, false);
        } else if (oTarget.attachEvent) {
            oTarget.attachEvent("on" + sEventType, fnHandler);
        } else {
            oTarget["on" + sEventType] = fnHandler;
        }
    };
     
    function removeEventHandler(oTarget, sEventType, fnHandler) {
        if (oTarget.removeEventListener) {
            oTarget.removeEventListener(sEventType, fnHandler, false);
        } else if (oTarget.detachEvent) {
            oTarget.detachEvent("on" + sEventType, fnHandler);
        } else { 
            oTarget["on" + sEventType] = null;
        }
    };
     
     
     
    //缩放程序
    var SimpleResize = Class.create();
    SimpleResize.prototype = {
      initialize: function(obj, options) {
        this._obj = $(obj);
         
        //alert(this);
        this._fR = BindAsEventListener(this, this.Resize);
        this._fS = Bind(this, this.Stop);
         
        this._obj.style.position = "absolute";
      },
      
      Delpre:function(){
      
    addEventHandler(this._obj, "click", BindAsEventListener(this, this.KeyDel));   //如何把delobj传给KeyDel函数呢?

    },

      KeyDel:function(){

    //alert(this._obj.id);
      addEventHandler(document, "keydown", BindAsEventListener(this, this.Del));  //接下来需要销毁绑定以免重复执行  


    },

      Del:function(e){
      
      var keydel

    if(window.event) // IE
       {
       keydel = e.keyCode
       }
    else if(e.which) // Netscape/Firefox/Opera
       {
       keydel = e.which
       }
    if(keydel==46){


    removeEventHandler(this._obj,"click", BindAsEventListener(this, this.KeyDel));
    removeEventHandler(document, "keydown", BindAsEventListener(this, this.Del));  //目前无效
               //alert(this);
    deln[delnum]=this._obj.id;
    alert(delnum);                                             //目前的问题是keydown之后并没有销毁绑定
    delnum++;
    var mubiaoparent=this._obj.parentNode;
    var removemubiao=mubiaoparent.removeChild(this._obj);

       

    }
      
      },  
      //设置触发对象
      Set: function(resize, side) {
        var resize = $(resize), fun;
        if(!resize) return;
        switch (side.toLowerCase()) {
        case "up" :
            fun = this.Up;
            break;
        case "down" :
            fun = this.Down;
            break;
        case "left" :
            fun = this.Left;
            break;
        case "right" :
            fun = this.Right;
            break;
        case "left-up" :
            fun = this.LeftUp;
            break;
        case "right-up" :
            fun = this.RightUp;
            break;
        case "left-down" :
            fun = this.LeftDown;
            break;
        case "right-down" :
        default :
            fun = this.RightDown;
        };
        //alert(resize.id);
        addEventHandler(resize, "mousedown", BindAsEventListener(this, this.Start, fun));
      },
      //准备缩放
      Start: function(e, fun) {
     
        //防止冒泡(跟拖放配合时设置)
        e.stopPropagation ? e.stopPropagation() : (e.cancelBubble = true);
        //设置执行程序
        this._fun = fun;
         
     alert(this.id);
        this._styleWidth =  this._obj.clientWidth;
        this._styleHeight = this._obj.clientHeight;
        this._styleLeft = this._obj.offsetLeft;
        this._styleTop = this._obj.offsetTop;
         
        this._sideLeft = e.clientX - this._styleWidth;
        this._sideRight = e.clientX + this._styleWidth;
        this._sideUp = e.clientY - this._styleHeight;
        this._sideDown = e.clientY + this._styleHeight;
         
        this._fixLeft = this._styleWidth + this._styleLeft;
        this._fixTop = this._styleHeight + this._styleTop;
         
        addEventHandler(document, "mousemove", this._fR);
        addEventHandler(document, "mouseup", this._fS);
            if(isIE){
            addEventHandler(this._obj, "losecapture", this._fS);
            this._obj.setCapture();
        }else{
            addEventHandler(window, "blur", this._fS);
            e.preventDefault();
        }
      },  
      //缩放
      Resize: function(e) {
        this._fun(e);
        with(this._obj.style){
            width = this._styleWidth + "px"; height = this._styleHeight + "px";
            top = this._styleTop + "px"; left = this._styleLeft + "px";
        }
      },
      //缩放程序
      //上
      Up: function(e) {
       
        this._styleHeight = Math.max(this._sideDown - e.clientY, 0);
        this._styleTop = this._fixTop - this._styleHeight;
      },
      //下
      Down: function(e) {
       
        this._styleHeight = Math.max(e.clientY - this._sideUp, 0);
      },
      //右
      Right: function(e) {
        this._styleWidth = Math.max(e.clientX - this._sideLeft, 0);
      },
      //左
      Left: function(e) {
        this._styleWidth = Math.max(this._sideRight - e.clientX, 0);
        this._styleLeft = this._fixLeft - this._styleWidth;
      },
      //右下
      RightDown: function(e) {
        this.Right(e); this.Down(e);
      },
      //右上
      RightUp: function(e) {
        this.Right(e); this.Up(e);
      },
      //左下
      LeftDown: function(e) {
        this.Left(e); this.Down(e);
      },
      //左上
      LeftUp: function(e) {
        this.Left(e); this.Up(e);
      },
       
       
      //停止缩放
      Stop: function() {
        removeEventHandler(document, "mousemove", this._fR);
        removeEventHandler(document, "mouseup", this._fS);
           if(isIE){
            removeEventHandler(this._obj, "losecapture", this._fS);
            this._obj.releaseCapture();
        }else{
            removeEventHandler(window, "blur", this._fS);
        }
      
      
      }
     
       
    };
     
    </script>
     
    <script type="text/javascript" src="Drag.js"></script>
    <style type="text/css">
    .rRightDown,.rLeftDown,.rLeftUp,.rRightUp,.rRight,.rLeft,.rUp,.rDown{
        position:absolute;
        background:#C00;
        width:7px;
        height:7px;
        z-index:5;
        font-size:0;
    }
     
    .rLeftDown,.rRightUp{cursor:ne-resize;}
    .rRightDown,.rLeftUp{cursor:nw-resize;}
    .rRight,.rLeft{cursor:e-resize;}
    .rUp,.rDown{cursor:n-resize;}
     
    .rLeftDown{left:-4px;bottom:-4px;}
    .rRightUp{right:-4px;top:-4px;}
    .rRightDown{right:-4px;bottom:-4px;background-color:#00F;}
    .rLeftUp{left:-4px;top:-4px;}
    .rRight{right:-4px;top:50%;margin-top:-4px;}
    .rLeft{left:-4px;top:50%;margin-top:-4px;}
    .rUp{top:-4px;left:50%;margin-left:-4px;}
    .rDown{bottom:-4px;left:50%;margin-left:-4px;}
     
    .jiantou{position:absolute;left:0px;top:0px;width:100%;height:100%;z-Index:-1;}
    .drag{ width:100px; height:60px; top:50px; left:50px;}
    </style>
     
    <div>  <input type="button" value="创建" onclick="createtuodong()">
     
    </div>
     
    <div id="tuodongqun">
     
    </div>
     
     
     
     
    <script>
     
    winid=1;
    i=80;
     
    function createtuodong(){
    var dd1 = document.createElement("div");
     
    dd1.id="dragDiv"+winid.toString();
    dd1.className="drag";
     
    dd1.style.left=(15+i).toString()+"px";
    dd1.style.top=(15+i).toString()+"px";
    i=i+50;
     
    var jiantoudiv = document.createElement("div");
    var jiantoupic = document.createElement("img");
    jiantoupic.className="jiantou";
    jiantoupic.id="jiantou";
    jiantoupic.src="jiantou1.png";
     
    var dd2 = document.createElement("div");
    dd2.id="rRightDown"+winid;
    dd2.className="rRightDown";
    var dd3 = document.createElement("div");
    dd3.id="rLeftDown"+winid;
    dd3.className="rLeftDown";
    var dd4 = document.createElement("div");
    dd4.id="rRightUp"+winid;
    dd4.className="rRightUp";
    var dd5 = document.createElement("div");
    dd5.id="rLeftUp"+winid;
    dd5.className="rLeftUp";
    var dd6 = document.createElement("div");
    dd6.id="rRight"+winid;
    dd6.className="rRight";
    var dd7 = document.createElement("div");
    dd7.id="rLeft"+winid;
    dd7.className="rLeft";
    var dd8 = document.createElement("div");
    dd8.id="rUp"+winid;
    dd8.className="rUp";
    var dd9 = document.createElement("div");
    dd9.id="rDown"+winid;
    dd9.className="rDown";
     
    dd1.appendChild(jiantoudiv);
    jiantoudiv.appendChild(jiantoupic);
    dd1.appendChild(dd2);
    dd1.appendChild(dd3);
    dd1.appendChild(dd4);
    dd1.appendChild(dd5);
    dd1.appendChild(dd6);
    dd1.appendChild(dd7);
    dd1.appendChild(dd8);
    dd1.appendChild(dd9);
     
     
    document.getElementById("tuodongqun").appendChild(dd1);//添加
     
     
    var rs = new SimpleResize(dd1.id);
     
    rs.Set(dd2.id, "right-down");
    rs.Set(dd3.id, "left-down");
     
    rs.Set(dd4.id, "right-up");
    rs.Set(dd5.id, "left-up");
     
    rs.Set(dd6.id, "right");
    rs.Set(dd7.id, "left");
     
    rs.Set(dd8.id, "up");
    rs.Set(dd9.id, "down");
     
    rs.Delpre();
     
    new Drag(dd1.id);
    winid++;
     
    }
    </script>
    </body>
    </html>
      

  4.   

    改了些地方,好像可以了。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>拖拉缩放效果</title>
    </head>
      
    <body>
      
    <script>
    var deln=new Array();
    var delnum=0; 
      
    var isIE = (document.all) ? true : false;
    var events = []; 
    var $ = function (id) {
        return "string" == typeof id ? document.getElementById(id) : id;
    };
      
    var Class = {
        create: function() {
            return function() { this.initialize.apply(this, arguments); }
        }
    }
      
    var Extend = function(destination, source) {
        for (var property in source) {
            destination[property] = source[property];
        }
    }
      
    var Bind = function(object, fun) {
        return function() {
            return fun.apply(object, arguments);
        }
    }
      
    var BindAsEventListener = function(object, fun) {
        var args = Array.prototype.slice.call(arguments,2);
        //alert(args);
        return function(event) {
            return fun.apply(object, [event || window.event].concat(args));
        }
    }
      
    var CurrentStyle = function(element){
        return element.currentStyle || document.defaultView.getComputedStyle(element, null);
    }
     
      
    function addEventHandler(oTarget, sEventType, fnHandler) {
        if (oTarget.addEventListener) {
            oTarget.addEventListener(sEventType, fnHandler, false);
        } else if (oTarget.attachEvent) {
            oTarget.attachEvent("on" + sEventType, fnHandler);
        } else {
            oTarget["on" + sEventType] = fnHandler;
        }
    };
      
    function removeEventHandler(oTarget, sEventType, fnHandler) {
        if (oTarget.removeEventListener) {
            oTarget.removeEventListener(sEventType, fnHandler, false);
        } else if (oTarget.detachEvent) {
            oTarget.detachEvent("on" + sEventType, fnHandler);
        } else { 
            oTarget["on" + sEventType] = null;
        }
    };
      
      
      
    //缩放程序
    var SimpleResize = Class.create();
    SimpleResize.prototype = {
      initialize: function(obj, options) {
        this._obj = $(obj);
          
        //alert(this);
        this._fR = BindAsEventListener(this, this.Resize);
        this._fS = Bind(this, this.Stop);
          
        this._obj.style.position = "absolute";
      },
       
      Delpre:function(){
        addEventHandler(this._obj, "click", BindAsEventListener(this, this.KeyDel));   //如何把delobj传给KeyDel函数呢?
         
        },
         
      KeyDel:function(){
        events[this.index] = BindAsEventListener(this, this.Del);
       addEventHandler(document, "keydown", events[this.index]);  //接下来需要销毁绑定以免重复执行
     },
         
      Del:function(e){
       
      var keydel
         
        if(window.event) // IE
          {
           keydel = e.keyCode
          }
        else if(e.which) // Netscape/Firefox/Opera
          {
           keydel = e.which
          }
            if(keydel==46){          
            removeEventHandler(this._obj,"click", BindAsEventListener(this, this.KeyDel));
            removeEventHandler(document, "keydown", events[this.index]);  //目前无效
               //alert(this);
            deln[delnum]=this._obj.id;
            alert(delnum);                                             //目前的问题是keydown之后并没有销毁绑定
            delnum++;        
            var mubiaoparent=this._obj.parentNode;
            var removemubiao=mubiaoparent.removeChild(this._obj);
            }
      },
     
       
      //设置触发对象
      Set: function(resize, side) {
        var resize = $(resize), fun;
        if(!resize) return;
        switch (side.toLowerCase()) {
        case "up" :
            fun = this.Up;
            break;
        case "down" :
            fun = this.Down;
            break;
        case "left" :
            fun = this.Left;
            break;
        case "right" :
            fun = this.Right;
            break;
        case "left-up" :
            fun = this.LeftUp;
            break;
        case "right-up" :
            fun = this.RightUp;
            break;
        case "left-down" :
            fun = this.LeftDown;
            break;
        case "right-down" :
        default :
            fun = this.RightDown;
        };
        //alert(resize.id);
        addEventHandler(resize, "mousedown", BindAsEventListener(this, this.Start, fun));
      },
      //准备缩放
      Start: function(e, fun) {
      
        //防止冒泡(跟拖放配合时设置)
        e.stopPropagation ? e.stopPropagation() : (e.cancelBubble = true);
        //设置执行程序
        this._fun = fun;
          
         alert(this.id);
        this._styleWidth =  this._obj.clientWidth;
        this._styleHeight = this._obj.clientHeight;
        this._styleLeft = this._obj.offsetLeft;
        this._styleTop = this._obj.offsetTop;
          
        this._sideLeft = e.clientX - this._styleWidth;
        this._sideRight = e.clientX + this._styleWidth;
        this._sideUp = e.clientY - this._styleHeight;
        this._sideDown = e.clientY + this._styleHeight;
          
        this._fixLeft = this._styleWidth + this._styleLeft;
        this._fixTop = this._styleHeight + this._styleTop;
          
        addEventHandler(document, "mousemove", this._fR);
        addEventHandler(document, "mouseup", this._fS);
            if(isIE){
            addEventHandler(this._obj, "losecapture", this._fS);
            this._obj.setCapture();
        }else{
            addEventHandler(window, "blur", this._fS);
            e.preventDefault();
        }
      },  
      //缩放
      Resize: function(e) {
        this._fun(e);
        with(this._obj.style){
            width = this._styleWidth + "px"; height = this._styleHeight + "px";
            top = this._styleTop + "px"; left = this._styleLeft + "px";
        }
      },
      //缩放程序
      //上
      Up: function(e) {
        
        this._styleHeight = Math.max(this._sideDown - e.clientY, 0);
        this._styleTop = this._fixTop - this._styleHeight;
      },
      //下
      Down: function(e) {
        
        this._styleHeight = Math.max(e.clientY - this._sideUp, 0);
      },
      //右
      Right: function(e) {
        this._styleWidth = Math.max(e.clientX - this._sideLeft, 0);
      },
      //左
      Left: function(e) {
        this._styleWidth = Math.max(this._sideRight - e.clientX, 0);
        this._styleLeft = this._fixLeft - this._styleWidth;
      },
      //右下
      RightDown: function(e) {
        this.Right(e); this.Down(e);
      },
      //右上
      RightUp: function(e) {
        this.Right(e); this.Up(e);
      },
      //左下
      LeftDown: function(e) {
        this.Left(e); this.Down(e);
      },
      //左上
      LeftUp: function(e) {
        this.Left(e); this.Up(e);
      },
        
        
      //停止缩放
      Stop: function() {
        removeEventHandler(document, "mousemove", this._fR);
        removeEventHandler(document, "mouseup", this._fS);
           if(isIE){
            removeEventHandler(this._obj, "losecapture", this._fS);
            this._obj.releaseCapture();
        }else{
            removeEventHandler(window, "blur", this._fS);
        }
       
       
      }
      
        
    };
      
    </script>
    <style type="text/css">
    .rRightDown,.rLeftDown,.rLeftUp,.rRightUp,.rRight,.rLeft,.rUp,.rDown{
        position:absolute;
        background:#C00;
        width:7px;
        height:7px;
        z-index:5;
        font-size:0;
    }
      
    .rLeftDown,.rRightUp{cursor:ne-resize;}
    .rRightDown,.rLeftUp{cursor:nw-resize;}
    .rRight,.rLeft{cursor:e-resize;}
    .rUp,.rDown{cursor:n-resize;}
      
    .rLeftDown{left:-4px;bottom:-4px;}
    .rRightUp{right:-4px;top:-4px;}
    .rRightDown{right:-4px;bottom:-4px;background-color:#00F;}
    .rLeftUp{left:-4px;top:-4px;}
    .rRight{right:-4px;top:50%;margin-top:-4px;}
    .rLeft{left:-4px;top:50%;margin-top:-4px;}
    .rUp{top:-4px;left:50%;margin-left:-4px;}
    .rDown{bottom:-4px;left:50%;margin-left:-4px;}
      
    .jiantou{position:absolute;left:0px;top:0px;width:100%;height:100%;z-Index:-1;}
    .drag{ width:100px; height:60px; top:50px; left:50px;}
    </style>
      
    <div>  <input type="button" value="创建" onclick="createtuodong()">
      
    </div>
      
    <div id="tuodongqun">
      
    </div>
      
      
      
      
    <script>
      
    winid=1;
    i=80;
      
    function createtuodong(){
    var dd1 = document.createElement("div");
      
    dd1.id="dragDiv"+winid.toString();
    dd1.className="drag";
      
    dd1.style.left=(15+i).toString()+"px";
    dd1.style.top=(15+i).toString()+"px";
    i=i+50;
      
    var jiantoudiv = document.createElement("div");
    var jiantoupic = document.createElement("img");
    jiantoupic.className="jiantou";
    jiantoupic.id="jiantou";
    jiantoupic.src="bbhh.jpg";
      
    var dd2 = document.createElement("div");
    dd2.id="rRightDown"+winid;
    dd2.className="rRightDown";
    var dd3 = document.createElement("div");
    dd3.id="rLeftDown"+winid;
    dd3.className="rLeftDown";
    var dd4 = document.createElement("div");
    dd4.id="rRightUp"+winid;
    dd4.className="rRightUp";
    var dd5 = document.createElement("div");
    dd5.id="rLeftUp"+winid;
    dd5.className="rLeftUp";
    var dd6 = document.createElement("div");
    dd6.id="rRight"+winid;
    dd6.className="rRight";
    var dd7 = document.createElement("div");
    dd7.id="rLeft"+winid;
    dd7.className="rLeft";
    var dd8 = document.createElement("div");
    dd8.id="rUp"+winid;
    dd8.className="rUp";
    var dd9 = document.createElement("div");
    dd9.id="rDown"+winid;
    dd9.className="rDown";
      
    dd1.appendChild(jiantoudiv);
    jiantoudiv.appendChild(jiantoupic);
    dd1.appendChild(dd2);
    dd1.appendChild(dd3);
    dd1.appendChild(dd4);
    dd1.appendChild(dd5);
    dd1.appendChild(dd6);
    dd1.appendChild(dd7);
    dd1.appendChild(dd8);
    dd1.appendChild(dd9);
      
      
    document.getElementById("tuodongqun").appendChild(dd1);//添加
      
      
    var rs = new SimpleResize(dd1.id);
      
    rs.Set(dd2.id, "right-down");
    rs.Set(dd3.id, "left-down");
      
    rs.Set(dd4.id, "right-up");
    rs.Set(dd5.id, "left-up");
      
    rs.Set(dd6.id, "right");
    rs.Set(dd7.id, "left");
      
    rs.Set(dd8.id, "up");
    rs.Set(dd9.id, "down");
    rs.index = winid;  
    rs.Delpre();
      
    //new Drag(dd1.id);
    winid++;
      
    }
    </script>
    </body>
    </html>