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键又会去删除已经被删除的东西,我认为是销毁有问题,不过不知道是为什么的问题,高手帮忙阿
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键又会去删除已经被删除的东西,我认为是销毁有问题,不过不知道是为什么的问题,高手帮忙阿
解决方案 »
- 急 window.open()打开窗口 在打开窗口后刷新页面 问一下新打开的页面先显示呢 还是2个页面一起显示呢?
- 请问 type=file,我不想要那个按钮,就写个文字,或者图片可以吗?
- input内的事件触发问题
- JS表单验证问题
- 有没有办法可以按一下回车键连续跳多个TAB呢?我写了个函数只能跳到下一格.
- 请教 如何得到 一个时间的从1970起的毫秒数
- 用JavaScript 控制提供用户输入的表格的问题?如各位能提供好思路,另开贴送分!
- 用JS可以调用出IE的打印预览窗口吗? 还有我从一个打印控制的问题,大家帮忙看看,急啊,等等等
- 关于WEB中对图片的处理问题,请教
- 更新js文件内容,但是没有改js名字,怎么能让已经引用这个js的网页强制刷新呢?
- ajax的send函数怎么总是出错?谁来看看我的代码有什么问题,帮改正下
- 问个基础的问题,一段代码除了函数定义和变量定义外,剩下的叫什么啊? 过程代码吗?
<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>
<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>