我有一个具有以下结构的表:
<table>
<tr>
<td><a href = "#">link1</a></td>
<td><a href = "#">link2</a></td>
<td><a href = "#">link3</a></td>
<td><input type = "checkbox" onclick = "func()"></td>
</tr>
</table>
function func(){
//I have to alert link1 here.
}
谁能告诉我该怎么做?
提前致谢。
编辑 1:有多行相同类型,单击特定复选框应提醒相应的 <a>
文本。
最佳答案
你可以使用jquery
来做到这一点。只需更改 eq
的编号即可。所有带有 input
和 checkbox
的 class
的表都将运行。你可以玩它。
$('.checkbox').on('click',function(){
var e = $(this).closest('table').find('td a');
alert(e.eq(0).text());
});
table{
border: 1px solid red;
margin-bottom: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table1">
<tr>
<td><a href = "#">link1</a></td>
<td><a href = "#">link2</a></td>
<td><a href = "#">link3</a></td>
<td><input type = "checkbox" class="checkbox"></td>
</tr>
</table>
<table class="table2">
<tr>
<td><a href = "#">link4</a></td>
<td><a href = "#">link5</a></td>
<td><a href = "#">link6</a></td>
<td><input type = "checkbox" class="checkbox"></td>
</tr>
</table>
关于javascript - 遍历 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36076889/