如何从html中获取所选行的值?

时间:2011-07-07 12:28:38

标签: javascript html

当我们点击一​​行时,我想得到一个表的选定行值,我该怎么做?

1 个答案:

答案 0 :(得分:1)

<script type="text/javascript">
window.onload=function() {
  var table = document.getElementById("table1");
  var tablerows = table.rows;
  for (var i=0, n=tablerows.length;i<n;i++) {
    tablerows[i].onclick=function() {
      alert(tablerows[i].textContent?tablerows[i].textContent:tablerows[i].innerText)
    }
  }
}
</script>