JQuery动态table添加行删除行

源:

评:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>动态添加行-删除行</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html" charset="gb2312">
<script src="js/jquery-1.9.0.js"></script>
<script>
//在id为test的table的最后增加一行
function addtr(id){
tr_id = $("#test>tbody>tr:last").attr("id");
tr_id++;
//alert(tr_id);
str = "<tr id = '"+tr_id+"'><td width='30%'>re1</td><td width='30%'>re2</td><td width='30%'>re3</td></tr>";
$('#'+id).append(str);
}
//删除id为test的table的最后一行
function deltr(id){
tr_id = $("#test>tbody>tr:last").attr("id");
$('#'+tr_id).remove();
}
</script>
</HEAD>


<BODY>
<table border="1px #ooo" id="test" name="test" class="test" cellpadding="0"

cellspacing="0" width="20%">
<tr id="1"><td width="30%">1</td><td width="30%">2</td><td width="30%">3</td></tr>

<tr id="2"><td width="30%">11</td><td width="30%">22</td><td width="30%">33</td></tr>

</table>

<input type="button" name="button" value="add" onclick="addtr('test');">
<input type="button" name="button" value="del" onclick="deltr('test');">




</BODY>
</HTML>

猜你喜欢

转载自mauersu.iteye.com/blog/1838208