当前位置: 首页 > 工具软件 > Tablecloth.js > 使用案例 >

js对table的操作

爱炯
2023-12-01

因为table里面tr,table这些个元素,不支持innerHTML,outerHTML,td也只支持innerHTML而已,所以我们就必需使用table.rows.insertRow deleteRow,之类的,好在这些方法可以实现在任何行插入,
var producttable=tableElement;
var temsss = producttable.insertRow(1);
var temsss2 = producttable.insertRow(2);
var cellCount = productselected.cells.length; 
var  cellCount2 = productselected.nextSibling.cells.length; 

temsss.id=productselected.id;
temsss.type=productselected.type;
temsss.style.display=productselected.style.display;
for(var j=0;j<cellCount;j++)            
{      
   temsss.insertCell(j).innerHTML=productselected.cells[j].innerHTML;
   temsss.cells(j).className= productselected.cells[j].className;
}   
for(var k=0;k<cellCount2;k++)            
{      
   temsss2.insertCell(k).innerHTML=productselected.nextSibling.cells[k].innerHTML;
   temsss2.cells(k).className= productselected.nextSibling.cells[k].className;
   temsss2.cells(k).colSpan= productselected.nextSibling.cells[k].colSpan;   
   temsss2.cells(k).name= productselected.nextSibling.cells[k].name;   
}  

但也有不爽的时候,比如我们对tr加了很多自义定属性,或是样式什么,都要手工去赋值,所以竟然少点属性,如果是样式的话,因为style=style是不行的,所以就用classname,这些东西要注意,老会有中间字大写的情况

另外,用innerHTML或outerHTML会把自定义的标签搞没,比如你定义了个<abc>,innerHTMl后,你会发现<abc>没有了

所以我们要用一些比较少的标签做定位,如<cite>

 类似资料: