<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function deleteRows(e){
var deleteRows =document.getElementById(e);
var deleteRowsIndex =deleteRows.rowIndex;
document.getElementById("myTable").deleteRow(deleteRowsIndex);
}
function addRow(){
var newRow=document.getElementById("myTable").insertRow(3);
var col1=newRow.insertCell(0);
col1.innerHTML="葡萄";
var col2=newRow.insertCell(1);
col2.innerHTML="10";
var col3=newRow.insertCell(2);
col3.innerHTML="5";
var col4=newRow.insertCell(3);
col4.innerHTML="<input type='button' value='删除' onclick='delRow()'/>"
}
function delRows(){
document.getElementById("myTable").deleteRow(1);
}
function getRows(){
var tb=document.getElementById("myTable");
alert(tb.rows.length);
}
function getCells(){
var tb=document.getElementById("myTable");
var tr=tb.rows[1];
var tdCount =tr.cells.length;
alert(tdCount);
}
</script>
</head>
<body>
<table width="275" border="1" id="myTable">
<thead>
<td width="71" height="33">商品名称</td>
<td width="51">价格</td>
<td width="47">数量</td>
<td width="78"></td>
</thead>
<tbody>
<tr id="aa">
<td height="25">苹果</td>
<td>2</td>
<td>10</td>
<td><input type="button" value="删除" onclick="deleteRows('aa')" /></td>
</tr>
<tr id="aaa">
<td height="25">香蕉</td>
<td>5</td>
<td>10</td>
<td><input type="button" value="删除" onclick="deleteRows('aaa')" /></td>
</tr>
</tbody>
</table>
<input type="button" value="增加行" onclick="addRow()"/>
<input type="button" value="总行数" onclick="getRows()"/>
<input type="button" value="第二行单元格" onclick="getCells()"/>
<input type="button" value="删除第二行" onclick="delRows()"/>
</body>
</html>
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/132769
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.