//動態(tài)添加行
function addRow(){
var table = document.getElementById("tableID");
var newRow = table.insertRow(); //創(chuàng)建新行
var newCell1 = newRow.insertCell(); //創(chuàng)建新單元格
newCell.innerHTML = ""; //單元格內(nèi)的內(nèi)容
newCell.setAttribute("align","center"); //設置位置
}
//動態(tài)刪除行
function deleteRow(){
var rowIndex = event.srcElement.parentElement.parentElement.rowIndex;
var styles = document.getElementById("tableID");
styles.deleteRow(rowIndex);
}
用克隆的方式
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
var index = 0;
$(document).ready(function(){
$("button").click(function(){
index++;
$("body").append($("p").clone().attr({'id':'name'+index,'name':'pwd'+index}));
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>復制每個 p 元素,然后追加到 body 元素</button>
</body>
</html>
下面是直接添加和刪除當前table表格,很好用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<a href = "javascript:void(0);" οnclick="a();">添加</a>
<a href = "javascript:void(0);" οnclick="b();">顯示</a>
<table id = "tab"></table>
</body>
<script type="text/javascript">
var index = 0;
var a = function(){
index++;
var tab = "<table id = 'tab_"+index+"' border = '1' >";
tab += "<tr><td colspan = '6' align = 'center'>尺寸規(guī)格</td></tr>";
tab += "<tr><td>長度</td><td><input οnblur='b("+index+");' id = 'cd_"+index+"' /></td><td align = 'right'>寬度</td><td><input οnblur='b("+index+");' id = 'kd_"+index+"' /></td><td>數(shù)量(個)</td><td><input οnblur='c("+index+");' id = 'sl_"+index+"' /></td></tr>";
tab += "<tr><td>擺放區(qū)域</td><td><input id = 'bfqy_"+index+"' /></td><td>單個面積(平方米)</td><td><input readonly = 'readonly' id = 'dgmj_"+index+"' /></td><td>總面積</td><td><input readonly = 'readonly' id = 'zmj_"+index+"' /></td></tr>";
tab += "<tr><td>內(nèi)容描述</td><td colspan = '4'><textarea rows='3' cols='70' id = 'content_"+index+"' ></textarea></td><td><input type = 'button' value = '刪除' onclick = 'del("+index+");' id = 'del_"+index+"' /></td></tr>";
tab += "</table>";
$('#tab').append(tab);
}
function del(ind){
$('#tab_'+ind).remove();
}
function b(ind){
var cdd = $('#cd_'+ind).val();
var kdd = $('#kd_'+ind).val();
if(cdd==''){cdd = 1;}
if(kdd==''){kdd = 1;}
if(cdd==''&&kdd==''){
$('#dgmj_'+ind).val('');
}else{
$('#dgmj_'+ind).val(cdd * kdd);
}
}
function c(ind){
var cdd = $('#cd_'+ind).val();
var kdd = $('#kd_'+ind).val();
var sll = $('#sl_'+ind).val();
if(cdd==''){cdd = 1;}
if(kdd==''){kdd = 1;}
if(sll==''){sll = 1;}
if(cdd==''&&kdd==''&&sll==''){
$('#zmj_'+ind).val('');
}else if(cdd!=''&&kdd!=''&&sll!=''){
$('#zmj_'+ind).val(cdd * kdd * sll);
}else{
$('#zmj_'+ind).val('');
}
}
</script>
</html>