js 动态增加/删除表格行

 

<table id="friend_comment-table" width="80%" >
<tr>
<td class="label">网友点评:</td>
<td>
<a href="#" onclick="addLadder(this,'friend_comment-table')"><strong>[+]</strong></a>

<table>
<tr>
<td>
<textarea name="friend_desc[]" cols="85" rows="6">tre</textarea>
</td>
</tr>
</table>
</td>
</tr>
</table>

<script language="JavaScript">
<!--

var Browser = new Object();

Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument != 'undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") != - 1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != - 1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != - 1);

/**
* 新增一个价格阶梯
*/
function addLadder(obj,table_obj, amount, price)
{

var src = obj.parentNode.parentNode;

var idx = rowindex(src);
var tbl = document.getElementById(table_obj);
var row = tbl.insertRow(idx + 1);
var cell = row.insertCell(-1);
cell.innerHTML = '';
var cell = row.insertCell(-1);
cell.innerHTML = src.cells[1].innerHTML.replace(/(.*)(addLadder)(.*)(\[)(\+)/i, "$1removeLadder$3$4-");;
}

/**
* 删除一个价格阶梯
*/
function removeLadder(obj,table_obj){

var row = rowindex(obj.parentNode.parentNode);
var tbl = document.getElementById(table_obj);

tbl.deleteRow(row);
}

function rowindex(tr){
  if (Browser.isIE){
    return tr.rowIndex; //针对IE
  }else{
    table = tr.parentNode.parentNode;
    for (i = 0; i < table.rows.length; i ++ ){
      if (table.rows[i] == tr){
        return i;
      }
    }
  }
}
//-->

</script>

posted @ 2012-02-10 15:08  taek  阅读(297)  评论(0编辑  收藏  举报