Js删除table内的某一行

来源:互联网 发布:盟军敢死队 mac 编辑:程序博客网 时间:2024/05/17 04:58
<span style="font-size:18px;">转载文章: http://blog.sina.com.cn/s/blog_69398ed9010163kb.html</span>
<span style="font-size:18px;">十分有用的例子,感谢原作者!</span>
<html><head><title>1</title><script>//得到行对象function getRowObj(obj){var i = 0;while(obj.tagName.toLowerCase() != "tr"){obj = obj.parentNode;if(obj.tagName.toLowerCase() == "table")return null;}return obj;}//根据得到的行对象得到所在的行数function getRowNo(obj){var trObj = getRowObj(obj);var trArr = trObj.parentNode.children;for(var trNo= 0; trNo < trArr.length; trNo++){if(trObj == trObj.parentNode.children[trNo]){alert(trNo+1);}}}//删除行function delRow(obj){var tr = this.getRowObj(obj);if(tr != null){tr.parentNode.removeChild(tr);}else{throw new Error("the given object is not contained by the table");}}</script></head><body><table border = "1"><tr><td>A<input type="button" value="A" onclick="getRowNo(this)">getRowNo<td></tr><tr><td>B<input type="button" value="B" onclick="delRow(this)">delRow<td></tr><tr><td>C<input type="button" value="C" onclick="getRowNo(this)">getRowNo</td></tr><tr><td>D<input type="button" value="D" onclick="getRowNo(this)">getRowNo</td></tr></table></body><html>

1 0
原创粉丝点击