javascript - 遍历 html 表

标签 javascript jquery html

我有一个具有以下结构的表:

<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 的编号即可。所有带有 inputcheckboxclass 的表都将运行。你可以玩它。

$('.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/

相关文章:

html - 在输入元素下方显示输入标签

javascript - 在 AngularJS 中使用 ngSanitize 指令

javascript - 如何将幻灯片范围插入mysql?

javascript - 为什么我的对象没有将数据填充到图表中? (包括演示)

javascript - 在 Firefox 浏览器中悬停时,导航子菜单不可见?

jquery - 使用 jQuery 确定 div 的数量

html - Angular 。从 tinymce 注入(inject) html

javascript - Twitter Bootstrap 适用于触摸屏吗?

javascript - 嗨,我需要一个建议。如果我单击一个包含某些值的复选框..该值引用其他复选框

javascript - HTML 中的数据处理,无需服务器代码(仅使用 Javascript)