用Jquery写的一个拖拽的构造函数,为什么只能new一次,第二次时firebug显示XXX is not a constructor。想问问这是怎么回事。
下面是代码,运行时改下Jquery的地址:
<!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>drag</title>
<script type="text/javascript" src="jquery-1.6.3.js"></script>
<script type="text/javascript">
var drag=new drag(".drag");
var drg1=new drag(".drag1");
function drag(idORclass){
var dragObj=this;
this.idORclass=idORclass;
this.deltaX=0;
this.deltaY=0;
this.runMove=function(e){
dragObj.dragMove(e);
}
this.runStop=function(e){
dragObj.dragStop(e);
}
this.dragStart=function(dragEvent){
dragObj.deltaX=dragEvent.clientX-$(dragObj.idORclass).offset().left;
dragObj.deltaY=dragEvent.clientY-$(dragObj.idORclass).offset().top;
$(dragObj.idORclass).bind("mousemove",dragObj.runMove);
$(dragObj.idORclass).bind("mouseup",dragObj.runStop);
}
this.dragMove=function(dragEvent){
$(dragObj.idORclass).css({
"left":(dragEvent.clientX-dragObj.deltaX)+"px",
"top" :(dragEvent.clientY-dragObj.deltaY)+"px",
})
}
this.dragStop=function(dragEvent){
$(dragObj.idORclass).unbind("mousemove",dragObj.runMove);
$(dragObj.idORclass).unbind("mouseup",dragObj.runStop);
}
this.runDrag=function(idORclass,func){
$(document).ready(function(){
$(idORclass).bind("mousedown",function(e){
func(e)
});
})
}
this.runDrag(this.idORclass,this.dragStart);
}
</script>
</head>
<style type="text/css">.drag{
position:absolute;
width:100px;
height:100px;
background-color:#00FF00;}
.drag1{
position:absolute;
width:100px;
height:100px;
left:300px;
background-color:#00FF00;}
</style>
<body>
<div class="drag">drag me</div>
<div class="drag1">drag me</div>
</body>
</html>
下面是代码,运行时改下Jquery的地址:
<!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>drag</title>
<script type="text/javascript" src="jquery-1.6.3.js"></script>
<script type="text/javascript">
var drag=new drag(".drag");
var drg1=new drag(".drag1");
function drag(idORclass){
var dragObj=this;
this.idORclass=idORclass;
this.deltaX=0;
this.deltaY=0;
this.runMove=function(e){
dragObj.dragMove(e);
}
this.runStop=function(e){
dragObj.dragStop(e);
}
this.dragStart=function(dragEvent){
dragObj.deltaX=dragEvent.clientX-$(dragObj.idORclass).offset().left;
dragObj.deltaY=dragEvent.clientY-$(dragObj.idORclass).offset().top;
$(dragObj.idORclass).bind("mousemove",dragObj.runMove);
$(dragObj.idORclass).bind("mouseup",dragObj.runStop);
}
this.dragMove=function(dragEvent){
$(dragObj.idORclass).css({
"left":(dragEvent.clientX-dragObj.deltaX)+"px",
"top" :(dragEvent.clientY-dragObj.deltaY)+"px",
})
}
this.dragStop=function(dragEvent){
$(dragObj.idORclass).unbind("mousemove",dragObj.runMove);
$(dragObj.idORclass).unbind("mouseup",dragObj.runStop);
}
this.runDrag=function(idORclass,func){
$(document).ready(function(){
$(idORclass).bind("mousedown",function(e){
func(e)
});
})
}
this.runDrag(this.idORclass,this.dragStart);
}
</script>
</head>
<style type="text/css">.drag{
position:absolute;
width:100px;
height:100px;
background-color:#00FF00;}
.drag1{
position:absolute;
width:100px;
height:100px;
left:300px;
background-color:#00FF00;}
</style>
<body>
<div class="drag">drag me</div>
<div class="drag1">drag me</div>
</body>
</html>
var drg1=new drag(".drag1");这样你第二个new的时候,引擎以为是红色那个drag
var drg1=new drag(".drag1");
没发现同名了么