正在查找表中单击的行的编号
Finding the number of the row that was clicked in table
我找到了一个代码,它可以获取被点击的行的编号,但我无法理解。
有人能解释一下下面的代码吗?
<html>
<head>
<title>Row indexes</title>
<script type="text/javascript">
onload = function() {
if (!document.getElementsByTagName || !document.createTextNode) return;
var rows = document.getElementById('my_table').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
for (i = 0; i < rows.length; i++) {
rows[i].onclick = function() {
alert(this.rowIndex + 1);
}
}
}
</script>
</head>
<body>
<table id="my_table">
<tbody>
<tr><td>first row</td></tr>
<tr><td>second row</td></tr>
</tbody>
</table>
</body>
</html>
URL为:http://webdesign.maratz.com/lab/row_index/
// Binding a function to a onload event and invokes it when
// window is loaded
onload = function () {
// Checking if the 2 methods are supposted by the browser.
// If no thne it returns and does nothing
if (!document.getElementsByTagName || !document.createTextNode) return;
// Finding the table by id and get the `tbody` tag inside it
// The get all the tr's based on tagName
var rows = document.getElementById('my_table')
.getElementsByTagName('tbody')[0]
.getElementsByTagName('tr');
// Iterate over all rows and bind a click event
for (i = 0; i < rows.length; i++) {
rows[i].onclick = function () {
alert(this.rowIndex + 1);
}
}
}
相关文章:
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 单击F5时如何停止页面加载
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 单击jquery清除输入值
- 单击按钮以等待单击按钮
- 在单击href链接的同时下载文件
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 使图像在单击时展开到不大于浏览器
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何在单击复选框后调用控制器方法
- 单击页面上的链接后高度发生变化
- 使用jquery在单击时在单元格中输入值
- 将纯文本URL转换为可单击链接
- ASP.NET通过单击JavaScript按钮触发c#事件
- 正在查找表中单击的行的编号
- 获取单击的元素父节点的数组编号
- 单击目录编号编辑项目
- HTML表格,单击行编号