国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

利用js動(dòng)態(tài)添加刪除table行的示例代碼

2019-11-20 21:27:29
字體:
供稿:網(wǎng)友

如下所示:

復(fù)制代碼 代碼如下:

//動(dòng)態(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"); //設(shè)置位置
}

//動(dòng)態(tài)刪除行
function deleteRow(){
   var rowIndex = event.srcElement.parentElement.parentElement.rowIndex;
   var styles = document.getElementById("tableID");
   styles.deleteRow(rowIndex);
}

<html>
<head>
<title></title>
</head>
<body>
<table id="testTbl" border=1>
<tr>
<td>
產(chǎn)品名稱
</td>
<td>
產(chǎn)品數(shù)量
</td>
<td>
產(chǎn)品單價(jià)
</td>
</tr>
<tr>
<td>
<select name="a">
   <option value="電子">電子</option>
   <option value="電器">電器</option>
</select></td>
<td>
    <input type="text" name="b">
     </td>
<td>
    <input type="text" name="c">
     </td>
</td>
</table>
<input type="button" name="Submit2" value="添加" onclick="addRow()">
<script>
function addRow(){
//添加行

var newTr = testTbl.insertRow();
//添加列
var newTd0 = newTr.insertCell();
var newTd1 = newTr.insertCell();
var newTd2 = newTr.insertCell();
var newTd3 = newTr.insertCell();
//設(shè)置列內(nèi)容和屬性

newTd0.innerText = document.all("a").options[document.all("a").selectedIndex].text;
newTd1.innerText = document.all("b").value;
newTd2.innerText = document.all("c").value;
newTd3.innerHTML= '<input type="button" name="del" value="刪除" onclick="del(this)">';
}
function del(o)
{
var   t=document.getElementById('testTbl');
t.deleteRow(o.parentNode.parentNode.rowIndex)
}
</script>
</body>
</html>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 刚察县| 雷山县| 丹棱县| 天津市| 康马县| 福鼎市| 远安县| 康定县| 咸丰县| 上虞市| 南部县| 肇州县| 旬阳县| 安徽省| 禹州市| 龙川县| 榕江县| 湛江市| 洛川县| 白朗县| 溧水县| 沂南县| 南阳市| 乌兰浩特市| 股票| 鄂尔多斯市| 衡阳县| 华宁县| 遂川县| 唐海县| 修文县| 庆阳市| 广丰县| 巴南区| 渭南市| 页游| 西城区| 蕉岭县| 太仓市| 全州县| 临洮县|