ITeye博客

js对行和列的操作

ozzzo · 2009-07-06 16:07 · 常用JavaScript · JavaScript · Markdown

<table id="testTbl" border=1>

<tr id="tr1">

<td width=6%><input type=checkbox id="box1"></td>

<td id="b">第一行</td>

</tr>

<tr id="tr2">

<td width=6%><input type=checkbox id="box2"></td>

<td id="b">第二行</td>

</tr>

</table>

 

 

动态添加表行的javascript函数如下:

function addRow(){

//添加一行

var newTr = testTbl.insertRow();

//添加两列

var newTd0 = newTr.insertCell();

var newTd1 = newTr.insertCell();

//设置列内容和属性

newTd0.innerHTML = '<input type=checkbox id="box4">';

newTd2.innerText= '新加行';

}

 

删除一行

 function delRow(){
  if(document.getElementById("files").rows.length >1){
   var newTr = document.getElementById("files").deleteRow();
  }else{
   return;
  }

 

其中document.getElement().rows.length  表示table 的行数

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。