因为DOM文档对象只有针对行(row)移动的操作,所以列移动只能自己写函数,通过单元格的循环移动来实现。
测试代码如下:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- </head>
- <body>
- <script language="javascript">
- function addCol(tableName,Row,Col)
- {
- var optTable=document.getElementById(tableName);
- var newRowObj = null;
- var newNameCell = null;
- for(i=0;i<Row;i++)
- {
- newRowObj = optTable.rows(i);
- for(j=1;j<=Col;j++)
- {
- newNameCell = newRowObj.insertCell(j);
- if(i==0){
- newNameCell.innerHTML = "tableColumn" + j;
- }
- else{
- newNameCell.innerHTML = "i am " + j +" col";
- }
- }
- }
-
- }
- function moveCol(tableName,startCol,endCol)
- {
- var optTable=document.getElementById(tableName);
- var newRowObj = null;
- var startCellObj = null;
- var endCellObj = null;
- var temp = null;
- for(i=0;i<optTable.rows.length;i++)
- {
- newRowObj = optTable.rows(i);
- startCellObj = newRowObj.cells(startCol);
- endCellObj = newRowObj.cells(endCol);
- temp=startCellObj.innerHTML;
- startCellObj.innerHTML=endCellObj.innerHTML;
- endCellObj.innerHTML=temp;
- }
-
- }
- </script>
- <table id="table1" border="2" onclick="moveCol(this.id,1,2)">
- <tr>
- <th>A</th><td><a href="##">A1</a></td><TD>A2</TD>
- </tr>
- <tr>
- <th>B</th><td>B1</td><TD>B2</TD>
- </tr>
- <tr>
- <th>C</th><TD>C1</TD><TD>C2</TD>
- </tr>
- </table>
- </body>
- </html>
复制代码
但是目前发现一个问题,通过读取数据库生成的表格,会提示没有对象的脚本错误。不知和解,请高手赐教。 |