javascript 一个很急的问题,各位大侠帮帮我啊!!!!

时间:2022-06-30 14:12:12
小弟是要完成一个网页拖拽功能,动态添加一个"table"但是要把该"table"的class属性设置成"dragTable",但是设置完之后不能拖拽啊!难道是mytable.setAttribute("class","dragTable");方法是错误的,还是class属性是只读的啊!动态添加table的代码如下:
     mytable = document.createElement("TABLE");
      mytablebody = document.createElement("TBODY");

mycurrent_row0=document.createElement("TR");
mycurrent_row1=document.createElement("TR");

mycurrent_cell0=document.createElement("TD");
         mycurrent_cell1=document.createElement("TD");
currenttext0=document.createTextNode(title);

mycurrent_cell0.setAttribute('bgcorlor','#ffffff');
alert(mycurrent_row0.getAttribute('bgColor'));
mycurrent_cell0.style.backgroundColor='#ffffff';
currenttext1=document.createTextNode(content);

mycurrent_cell0.appendChild(currenttext0);
mycurrent_cell1.appendChild(currenttext1);
                      mycurrent_row0.appendChild(mycurrent_cell0);
                      mycurrent_row1.appendChild(mycurrent_cell1);
                      mytablebody.appendChild(mycurrent_row0);
                      mytablebody.appendChild(mycurrent_row1);
                      mytable.appendChild(mytablebody);
                      mytable.setAttribute("border","1");
                      mytable.setAttribute("class","dragTable");
                      mytable.className="dragTable";
                     alert(mytable.getAttribute("class"));
                     mytable.setAttibute("cellspacing","0");
                     mytable.setAttribute("width","100%");
                     alert(mytable.getAttribute("width"));
                     mytable.setAttribute("height","50");
                     parentTable.cells[column].appendChild(mytable);


 

8 个解决方案

#1


其他整个代码如下:
<html>
<head>
<title>DRAG the DIV</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<style>
*{font-size:12px}
.dragTable{
 font-size:12px;
 border-top:1px solid #3366cc;
 margin-bottom: 10px;
 width:100%;
 background-color:#FFFFFF;
}

td{vertical-align:top;}

.dragTR{
 cursor:move;
 color:#7787cc;
 background-color:#e5eef9;
 height:20px;
 padding-left:5px;
 font-weight:bold;
}

#parentTable{
 border-collapse:collapse;
 letter-spacing:25px;
}

</style>

<script defer>

/****JoeLee************E-MAIL:hktx@163.com****<a class='Channel_KeyLink' href='http://www.51it.org/school/tools/tongxun/qq/index.html'>QQ</a>:48293707*****11:09 2006-2-9******/

 var Drag={
  dragged:false,
  ao:null,
  tdiv:null,
 
  dragStart:function(){
   Drag.ao=event.srcElement;

   if((Drag.ao.tagName=="TD")||(Drag.ao.tagName=="TR")){
    Drag.ao=Drag.ao.offsetParent;
    Drag.ao.style.zIndex=100;
   }else
    return;

  Drag.dragged=true;
  Drag.tdiv=document.createElement("div");
  Drag.tdiv.innerHTML=Drag.ao.outerHTML;
  Drag.ao.style.border="1px dashed red";
  Drag.tdiv.style.display="block";
  Drag.tdiv.style.position="absolute";
  Drag.tdiv.style.filter="alpha(opacity=70)";
  Drag.tdiv.style.cursor="move";
  Drag.tdiv.style.border="1px solid #000000";
  Drag.tdiv.style.width=Drag.ao.offsetWidth;
  Drag.tdiv.style.height=Drag.ao.offsetHeight;
  Drag.tdiv.style.top=Drag.getInfo(Drag.ao).top;
  Drag.tdiv.style.left=Drag.getInfo(Drag.ao).left;
  document.body.appendChild(Drag.tdiv);
  Drag.lastX=event.clientX;
  Drag.lastY=event.clientY;
  Drag.lastLeft=Drag.tdiv.style.left;
  Drag.lastTop=Drag.tdiv.style.top;
 },

 draging:function(){//&#65430;&#65432;&#65426;&#65386;:&#65413;&#65424;&#65398;&#65423;MOUSE&#65397;&#65412;&#65422;&#65403;&#65430;&#65411;

 if(!Drag.dragged||Drag.ao==null)return;

 var tX=event.clientX;

 var tY=event.clientY;

 Drag.tdiv.style.left=parseInt(Drag.lastLeft)+tX-Drag.lastX;

 Drag.tdiv.style.top=parseInt(Drag.lastTop)+tY-Drag.lastY;

 for(var i=0;i<parentTable.cells.length;i++){

  var parentCell=Drag.getInfo(parentTable.cells[i]);

  if(tX>=parentCell.left&&tX<=parentCell.right&&tY>=parentCell.top&&tY<=parentCell.bottom){

   var subTables=parentTable.cells[i].getElementsByTagName("table");

   if(subTables.length==0){

    if(tX>=parentCell.left&&tX<=parentCell.right&&tY>=parentCell.top&&tY<=parentCell.bottom){

     parentTable.cells[i].appendChild(Drag.ao);

    }

    break;

   }

   for(var j=0;j<subTables.length;j++){

    var subTable=Drag.getInfo(subTables[j]);

    if(tX>=subTable.left&&tX<=subTable.right&&tY>=subTable.top&&tY<=subTable.bottom){

     parentTable.cells[i].insertBefore(Drag.ao,subTables[j]);

     break;

    }else{

     parentTable.cells[i].appendChild(Drag.ao);

    } 

   }

  }

 }

}

,

 dragEnd:function(){

 if(!Drag.dragged)return;

 Drag.dragged=false;

 Drag.mm=Drag.repos(150,15);

 Drag.ao.style.borderWidth="0px";

 Drag.ao.style.borderTop="1px solid #3366cc";

 Drag.tdiv.style.borderWidth="0px";

 Drag.ao.style.zIndex=1;

},

getInfo:function(o){//&#65416;&#65377;&#65397;&#65411;&#65431;&#12539;

 var to=new Object();

 to.left=to.right=to.top=to.bottom=0;

 var twidth=o.offsetWidth;

 var theight=o.offsetHeight;

 while(o!=document.body){

  to.left+=o.offsetLeft;

  to.top+=o.offsetTop;

  o=o.offsetParent;

 }

  to.right=to.left+twidth;

  to.bottom=to.top+theight;

 return to;

},

repos:function(aa,ab){

 var f=Drag.tdiv.filters.alpha.opacity;

 var tl=parseInt(Drag.getInfo(Drag.tdiv).left);

 var tt=parseInt(Drag.getInfo(Drag.tdiv).top);

 var kl=(tl-Drag.getInfo(Drag.ao).left)/ab;

 var kt=(tt-Drag.getInfo(Drag.ao).top)/ab;

 var kf=f/ab;

 return setInterval(function(){if(ab<1){

       clearInterval(Drag.mm);

       Drag.tdiv.removeNode(true);

       Drag.ao=null;

       return;

      }

     ab--;

     tl-=kl;

     tt-=kt;

     f-=kf;

     Drag.tdiv.style.left=parseInt(tl)+"px";

     Drag.tdiv.style.top=parseInt(tt)+"px";

     Drag.tdiv.filters.alpha.opacity=f;

    }

,aa/ab)

},

 inint:function(){//&#65395;&#65404;&#65403;&#65391;

 for(var i=0;i<parentTable.cells.length;i++){
  var subTables=parentTable.cells[i].getElementsByTagName("table");

  for(var j=0;j<subTables.length;j++){
   if(subTables[j].className!="dragTable")break;
   subTables[j].rows[0].className="dragTR";
   subTables[j].rows[0].attachEvent("onmousedown",Drag.dragStart);
  }
 }

 document.onmousemove=Drag.draging;
 document.onmouseup=Drag.dragEnd;
}

//end of Object Drag

}

Drag.inint();
function _show(str){
 var w=window.open('','');
 var d=w.document;
 d.open();
 str=str.replace(/=(?!")(.*?)(?!")( |>)/g,"=\"$1\"$2");
 str=str.replace(/(<)(.*?)(>)/g,"<span style='color:red;'>&lt;$2&gt;</span><br />");
 str=str.replace(/\r/g,"<br />\n");
 d.write(str);
}
function insertTable(parentTable,column,title,content){
         //  var divDiv = document.createElement("div");
//divDiv.innerHTML = '<table cellpadding="0" cellspacing="0" width="100%" class="dragTable"><tr><td></td></tr></table>';
// parentTable.innerHTML='<table cellpadding="0" cellspacing="0" width="100 class="dragTable"><tr><td></td></tr></table>';
var nTable=document.createElement("table");
      // parentTableTR=document.createElement("TR");
      // parentTableTD=document.createElement("TD");
       
        mytable = document.createElement("TABLE");
        mytablebody = document.createElement("TBODY");

mycurrent_row0=document.createElement("TR");
mycurrent_row1=document.createElement("TR");

mycurrent_cell0=document.createElement("TD");
    mycurrent_cell1=document.createElement("TD");
currenttext0=document.createTextNode(title);

mycurrent_cell0.setAttribute('bgcorlor','#ffffff');
alert(mycurrent_row0.getAttribute('bgColor'));
mycurrent_cell0.style.backgroundColor='#ffffff';
currenttext1=document.createTextNode(content);

mycurrent_cell0.appendChild(currenttext0);
mycurrent_cell1.appendChild(currenttext1);
        mycurrent_row0.appendChild(mycurrent_cell0);
        mycurrent_row1.appendChild(mycurrent_cell1);

mytablebody.appendChild(mycurrent_row0);
mytablebody.appendChild(mycurrent_row1);
        // appends TBODY into TABLE
        mytable.appendChild(mytablebody);
        // sets the border attribute of mytable to 2;
        mytable.setAttribute("border","1");
        mytable.setAttribute("class","dragTable");
mytable.className="dragTable";

        alert(mytable.getAttribute("class"));
    //   mytable.setAttibute("cellspacing","0");
        mytable.setAttribute("width","100%");
       alert(mytable.getAttribute("width"));
mytable.setAttribute("height","50");

//parentTable.appendChild(parentTableTR);
//parentTable.appendChild(parentTableTD);
parentTable.cells[column].appendChild(mytable);
//window.location.reload();

}
function test1(){
str1+="parentTable.cells.length=";
str1+=document.all.parentTable.cells.length;
str1+="\n";

var subTables=parentTable.cells[0].getElementsByTagName("table");
str1+="第一个子表中的table数:";
str1+=subTables.length;
str1+="\n";

disp1.value=str1
}


function test2(){
var thecolor=document.getElementsByTagName("table");
var element=thecolor[1].attributes["class"].value;

var col=document.all.column.value;
var t=document.all.title.value;
var c=document.all.content.value;
 //  alert(thecolor);

insertTable(document.all.parentTable,col,t,c);

  }

</script>

</head>

#2


<body>
column:<input name="column"><br>
 title:<input name="title"><br>
 content:<input name="content"><br>
 <input type="button" value="增加一个table" onclick="return test2();"><br>
<input type="button" value="显示属性" onclick="return test1();" id=button2 name=button1><br>
 <table border="1" width="100%" height=500 id="parentTable">
 
<tr>
    <td>
<table border="1" width="100%" class="dragTable">
<tr>
<td bgcolor="green">gmail</td>
</tr>
<tr>
<td>login gmail</td>
</tr>
</table>

<table border="1" width="100%" class="dragTable">
<tr>
<td bgcolor="blue">天气情况</td>
</tr>
<tr>
<td>相当好</td>
</tr>
</table>


</td>
<td>
2
</td>
<td>
3
</td>
</tr>
 </table>

</body>

</html>

#3


很急啊!
都弄了3天了!
在线等!!!

#4


来接分! 你用用document.getElementById("").style=""来看一下

#5


dinggushan
您说的是什么意思啊

#6


.dragTable{
 font-size:12px;
 border-top:1px solid #3366cc;
 margin-bottom: 10px;
 width:100%;
 background-color:#FFFFFF;
 position:absolute;  /* add attribute */
 z-index:9;          /* add attribute */
}

#7


来支持兄弟下

#8


对象名.className=""

#1


其他整个代码如下:
<html>
<head>
<title>DRAG the DIV</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<style>
*{font-size:12px}
.dragTable{
 font-size:12px;
 border-top:1px solid #3366cc;
 margin-bottom: 10px;
 width:100%;
 background-color:#FFFFFF;
}

td{vertical-align:top;}

.dragTR{
 cursor:move;
 color:#7787cc;
 background-color:#e5eef9;
 height:20px;
 padding-left:5px;
 font-weight:bold;
}

#parentTable{
 border-collapse:collapse;
 letter-spacing:25px;
}

</style>

<script defer>

/****JoeLee************E-MAIL:hktx@163.com****<a class='Channel_KeyLink' href='http://www.51it.org/school/tools/tongxun/qq/index.html'>QQ</a>:48293707*****11:09 2006-2-9******/

 var Drag={
  dragged:false,
  ao:null,
  tdiv:null,
 
  dragStart:function(){
   Drag.ao=event.srcElement;

   if((Drag.ao.tagName=="TD")||(Drag.ao.tagName=="TR")){
    Drag.ao=Drag.ao.offsetParent;
    Drag.ao.style.zIndex=100;
   }else
    return;

  Drag.dragged=true;
  Drag.tdiv=document.createElement("div");
  Drag.tdiv.innerHTML=Drag.ao.outerHTML;
  Drag.ao.style.border="1px dashed red";
  Drag.tdiv.style.display="block";
  Drag.tdiv.style.position="absolute";
  Drag.tdiv.style.filter="alpha(opacity=70)";
  Drag.tdiv.style.cursor="move";
  Drag.tdiv.style.border="1px solid #000000";
  Drag.tdiv.style.width=Drag.ao.offsetWidth;
  Drag.tdiv.style.height=Drag.ao.offsetHeight;
  Drag.tdiv.style.top=Drag.getInfo(Drag.ao).top;
  Drag.tdiv.style.left=Drag.getInfo(Drag.ao).left;
  document.body.appendChild(Drag.tdiv);
  Drag.lastX=event.clientX;
  Drag.lastY=event.clientY;
  Drag.lastLeft=Drag.tdiv.style.left;
  Drag.lastTop=Drag.tdiv.style.top;
 },

 draging:function(){//&#65430;&#65432;&#65426;&#65386;:&#65413;&#65424;&#65398;&#65423;MOUSE&#65397;&#65412;&#65422;&#65403;&#65430;&#65411;

 if(!Drag.dragged||Drag.ao==null)return;

 var tX=event.clientX;

 var tY=event.clientY;

 Drag.tdiv.style.left=parseInt(Drag.lastLeft)+tX-Drag.lastX;

 Drag.tdiv.style.top=parseInt(Drag.lastTop)+tY-Drag.lastY;

 for(var i=0;i<parentTable.cells.length;i++){

  var parentCell=Drag.getInfo(parentTable.cells[i]);

  if(tX>=parentCell.left&&tX<=parentCell.right&&tY>=parentCell.top&&tY<=parentCell.bottom){

   var subTables=parentTable.cells[i].getElementsByTagName("table");

   if(subTables.length==0){

    if(tX>=parentCell.left&&tX<=parentCell.right&&tY>=parentCell.top&&tY<=parentCell.bottom){

     parentTable.cells[i].appendChild(Drag.ao);

    }

    break;

   }

   for(var j=0;j<subTables.length;j++){

    var subTable=Drag.getInfo(subTables[j]);

    if(tX>=subTable.left&&tX<=subTable.right&&tY>=subTable.top&&tY<=subTable.bottom){

     parentTable.cells[i].insertBefore(Drag.ao,subTables[j]);

     break;

    }else{

     parentTable.cells[i].appendChild(Drag.ao);

    } 

   }

  }

 }

}

,

 dragEnd:function(){

 if(!Drag.dragged)return;

 Drag.dragged=false;

 Drag.mm=Drag.repos(150,15);

 Drag.ao.style.borderWidth="0px";

 Drag.ao.style.borderTop="1px solid #3366cc";

 Drag.tdiv.style.borderWidth="0px";

 Drag.ao.style.zIndex=1;

},

getInfo:function(o){//&#65416;&#65377;&#65397;&#65411;&#65431;&#12539;

 var to=new Object();

 to.left=to.right=to.top=to.bottom=0;

 var twidth=o.offsetWidth;

 var theight=o.offsetHeight;

 while(o!=document.body){

  to.left+=o.offsetLeft;

  to.top+=o.offsetTop;

  o=o.offsetParent;

 }

  to.right=to.left+twidth;

  to.bottom=to.top+theight;

 return to;

},

repos:function(aa,ab){

 var f=Drag.tdiv.filters.alpha.opacity;

 var tl=parseInt(Drag.getInfo(Drag.tdiv).left);

 var tt=parseInt(Drag.getInfo(Drag.tdiv).top);

 var kl=(tl-Drag.getInfo(Drag.ao).left)/ab;

 var kt=(tt-Drag.getInfo(Drag.ao).top)/ab;

 var kf=f/ab;

 return setInterval(function(){if(ab<1){

       clearInterval(Drag.mm);

       Drag.tdiv.removeNode(true);

       Drag.ao=null;

       return;

      }

     ab--;

     tl-=kl;

     tt-=kt;

     f-=kf;

     Drag.tdiv.style.left=parseInt(tl)+"px";

     Drag.tdiv.style.top=parseInt(tt)+"px";

     Drag.tdiv.filters.alpha.opacity=f;

    }

,aa/ab)

},

 inint:function(){//&#65395;&#65404;&#65403;&#65391;

 for(var i=0;i<parentTable.cells.length;i++){
  var subTables=parentTable.cells[i].getElementsByTagName("table");

  for(var j=0;j<subTables.length;j++){
   if(subTables[j].className!="dragTable")break;
   subTables[j].rows[0].className="dragTR";
   subTables[j].rows[0].attachEvent("onmousedown",Drag.dragStart);
  }
 }

 document.onmousemove=Drag.draging;
 document.onmouseup=Drag.dragEnd;
}

//end of Object Drag

}

Drag.inint();
function _show(str){
 var w=window.open('','');
 var d=w.document;
 d.open();
 str=str.replace(/=(?!")(.*?)(?!")( |>)/g,"=\"$1\"$2");
 str=str.replace(/(<)(.*?)(>)/g,"<span style='color:red;'>&lt;$2&gt;</span><br />");
 str=str.replace(/\r/g,"<br />\n");
 d.write(str);
}
function insertTable(parentTable,column,title,content){
         //  var divDiv = document.createElement("div");
//divDiv.innerHTML = '<table cellpadding="0" cellspacing="0" width="100%" class="dragTable"><tr><td></td></tr></table>';
// parentTable.innerHTML='<table cellpadding="0" cellspacing="0" width="100 class="dragTable"><tr><td></td></tr></table>';
var nTable=document.createElement("table");
      // parentTableTR=document.createElement("TR");
      // parentTableTD=document.createElement("TD");
       
        mytable = document.createElement("TABLE");
        mytablebody = document.createElement("TBODY");

mycurrent_row0=document.createElement("TR");
mycurrent_row1=document.createElement("TR");

mycurrent_cell0=document.createElement("TD");
    mycurrent_cell1=document.createElement("TD");
currenttext0=document.createTextNode(title);

mycurrent_cell0.setAttribute('bgcorlor','#ffffff');
alert(mycurrent_row0.getAttribute('bgColor'));
mycurrent_cell0.style.backgroundColor='#ffffff';
currenttext1=document.createTextNode(content);

mycurrent_cell0.appendChild(currenttext0);
mycurrent_cell1.appendChild(currenttext1);
        mycurrent_row0.appendChild(mycurrent_cell0);
        mycurrent_row1.appendChild(mycurrent_cell1);

mytablebody.appendChild(mycurrent_row0);
mytablebody.appendChild(mycurrent_row1);
        // appends TBODY into TABLE
        mytable.appendChild(mytablebody);
        // sets the border attribute of mytable to 2;
        mytable.setAttribute("border","1");
        mytable.setAttribute("class","dragTable");
mytable.className="dragTable";

        alert(mytable.getAttribute("class"));
    //   mytable.setAttibute("cellspacing","0");
        mytable.setAttribute("width","100%");
       alert(mytable.getAttribute("width"));
mytable.setAttribute("height","50");

//parentTable.appendChild(parentTableTR);
//parentTable.appendChild(parentTableTD);
parentTable.cells[column].appendChild(mytable);
//window.location.reload();

}
function test1(){
str1+="parentTable.cells.length=";
str1+=document.all.parentTable.cells.length;
str1+="\n";

var subTables=parentTable.cells[0].getElementsByTagName("table");
str1+="第一个子表中的table数:";
str1+=subTables.length;
str1+="\n";

disp1.value=str1
}


function test2(){
var thecolor=document.getElementsByTagName("table");
var element=thecolor[1].attributes["class"].value;

var col=document.all.column.value;
var t=document.all.title.value;
var c=document.all.content.value;
 //  alert(thecolor);

insertTable(document.all.parentTable,col,t,c);

  }

</script>

</head>

#2


<body>
column:<input name="column"><br>
 title:<input name="title"><br>
 content:<input name="content"><br>
 <input type="button" value="增加一个table" onclick="return test2();"><br>
<input type="button" value="显示属性" onclick="return test1();" id=button2 name=button1><br>
 <table border="1" width="100%" height=500 id="parentTable">
 
<tr>
    <td>
<table border="1" width="100%" class="dragTable">
<tr>
<td bgcolor="green">gmail</td>
</tr>
<tr>
<td>login gmail</td>
</tr>
</table>

<table border="1" width="100%" class="dragTable">
<tr>
<td bgcolor="blue">天气情况</td>
</tr>
<tr>
<td>相当好</td>
</tr>
</table>


</td>
<td>
2
</td>
<td>
3
</td>
</tr>
 </table>

</body>

</html>

#3


很急啊!
都弄了3天了!
在线等!!!

#4


来接分! 你用用document.getElementById("").style=""来看一下

#5


dinggushan
您说的是什么意思啊

#6


.dragTable{
 font-size:12px;
 border-top:1px solid #3366cc;
 margin-bottom: 10px;
 width:100%;
 background-color:#FFFFFF;
 position:absolute;  /* add attribute */
 z-index:9;          /* add attribute */
}

#7


来支持兄弟下

#8


对象名.className=""