获取表格某一行的索引

Cool_xiaocao 2011-04-17 11:33:44
利用Javascript在表格的每一行动态添加了一个input 按钮,并且添加了相应的按钮点击事件
我想点击这个按钮就删除相应的行
我的思路是
实现删除行,Javascript提供了 deleteRow(index) 根据行索引删除行
可是,不知道怎么获取行的索引?请指教。

或者,有没有其它方法实现点击按钮实现删除行
...全文
430 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
乌镇程序员 2011-04-18
  • 打赏
  • 举报
回复
写成函数可以这样做:
<!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 rmRow(t) {
var idx = t.parentNode.parentNode.rowIndex;
document.getElementById('tb').deleteRow(idx);
}
</script>
</head>

<body>
<table id="tb">
<tr><td>TEST_ROW1</td><td><input type="button" onclick="rmRow(this);" value="REMOVE" /></td></tr>
<tr><td>TEST_ROW2</td><td><input type="button" onclick="rmRow(this);" value="REMOVE" /></td></tr>
</table>
</body>
</html>
pa0mm 2011-04-18
  • 打赏
  • 举报
回复
有谁知道如何禁止拉动浏览器大小?!吗
http://topic.csdn.net/u/20110418/22/7e5b17a5-e255-417b-8196-3f095064aaf8.html?21601
sohighthesky 2011-04-18
  • 打赏
  • 举报
回复
。。。2楼的没问题,lz仔细测
Cool_xiaocao 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mzytechsupport 的回复:]

JScript code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function deleteSelf(obj) {
var trobj = obj.parentNode.par……
[/Quote]

这位朋友也是,你的代码是无论点击哪一个按钮都是删掉最先的那一行
Cool_xiaocao 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 t5500 的回复:]

写成函数可以这样做:
HTML code
<!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>
<me……
[/Quote]

朋友,你的代码是无论点击哪一个按钮都是删掉最先的那一行
mzytechsupport 2011-04-18
  • 打赏
  • 举报
回复

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function deleteSelf(obj) {
var trobj = obj.parentNode.parentNode;
trobj.parentNode.removeChild(trobj);
}
</script>
</head>
<body>
<table>
<tr><td>1</td><td><a href="#@" onclick="deleteSelf(this);">删除自己</a></td></tr>
<tr><td>2</td><td><a href="#@" onclick="deleteSelf(this);">删除自己</a></td></tr>
<tr><td>3</td><td><a href="#@" onclick="deleteSelf(this);">删除自己</a></td></tr>
</table>
</body>
</html>
乌镇程序员 2011-04-17
  • 打赏
  • 举报
回复
<!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>
</head>

<body>
<table id="tb">
<tr><td>TEST</td><td><input type="button" onclick="document.getElementById('tb').deleteRow(this.parentNode.parentNode.rowIndex);" value="REMOVE" /></td></tr>
</table>
</body>
</html>

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧