0、前端页面上编写table,形如:
<table id="sync-table-id">
<tr>
<th class="sync-table-title-class sync-table-th-class" id="sync-table-title-id" colspan="6">从“xx店”同步</th>
</tr>
<tr style="background-color:#EEEEEE;">
<th><input id="selectAllCheckbox" οnchange="onSelectAllCheckbox()" checked="checked" type="checkbox"/></th>
<th class="sync-table-head-img-class sync-table-th-class">他店主图</th>
<th class="sync-table-head-name-class sync-table-th-class">他店宝贝</th>
<th class="sync-table-head-sync-class sync-table-th-class">同步</th>
<th class="sync-table-head-img-class sync-table-th-class">本店主图</th>
<th class="sync-table-head-name-class sync-table-th-class">本店宝贝</th>
</tr>
<tr>
<td><input type="checkbox"/></td>
<td>图片1</td>
<td>宝贝名称1<br/>宝贝描述1</td>
<td><img src="Images/ok.png"/></td>
<td>图片1<br/>图片1</td>
<td>宝贝名称1<br/>宝贝描述1</td>
</tr>
<tr>
<td><input type="checkbox"/></td>
<td>图片2</td>
<td>宝贝名称2</td>
<td><img src="Images/no.png"/></td>
<td>图片2</td>
<td>宝贝名称2</td>
</tr>
</table>
下面开始对这个table进行操作:
1、取得table对象:
var syncTableObj=findObj("sync-table-id",document);
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if((p = theObj.indexOf("?")) > 0 && parent.frames.length)
{
theDoc = parent.frames[theObj.substring(p+1)].document;
theObj = theObj.substring(0,p);
}
if(!(foundObj = theDoc[theObj]) && theDoc.all)
foundObj = theDoc.all[theObj];
for (i=0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById)
foundObj = document.getElementById(theObj);
return foundObj;
}
2、通过deleteRow删除行,下面这个function可以删除所有,只保留了最初两行:
function deleteAll()
{
for(var i=syncTableObj.rows.length-1;i>1;i--)
{
syncTableObj.deleteRow(i);
}
}
注意删除的时候要从底往上删除,因为rowindex随着删除而改变。
3、插入行:
var newTR=syncTableObj.insertRow(syncTableObj.rows.length);
var checkBox=newTR.insertCell(0);
var authPicUrl=newTR.insertCell(1);
var authItem=newTR.insertCell(2);
var hasSameItem=newTR.insertCell(3);
var selfPicUrl=newTR.insertCell(4);
var selfItem=newTR.insertCell(5);
注意:插入行要从0开始
赋值如下:
checkBox.innerHTML='<input type="checkbox" disabled="true"/>';