从JavaScript表格中获取价值...不是jQuery [英] Getting value from table cell in JavaScript...not jQuery

查看:82
本文介绍了从JavaScript表格中获取价值...不是jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法相信这会花费我多长时间,但我似乎无法弄清楚如何在JavaScript中迭代表格时从HTML表格中提取单元格值。我使用以下方法进行迭代:

  var refTab = document.getElementById(ddReferences)
var ttl;
//遍历表中的所有行和列,并弹出警报,其值为
// //每个单元格的内容。
for(var i = 0; row = refTab.rows [i]; i ++){
row = refTab.rows [i]; (var j = 0; col = row.cells [j]; j ++){
alert(col.firstChild.nodeValue);



我应该把什么正确的电话打入alert()调用来显示我的HTML表格的每个单元格的内容?这应该是在JS ...不能使用jQuery。

解决方案

 函数GetCellValues (){
var table = document.getElementById('mytable'); $(var r = 0,n = table.rows.length; r for(var c = 0,m = table.rows [r] .cells.length ; c< m; c ++){
alert(table.rows [r] .cells [c] .innerHTML);
}
}
}


I can't believe how long this has taken me but I can't seem to figure out how to extract a cell value from an HTML table as I iterate through the table with JavaScript. I am using the following to iterate:

  var refTab=document.getElementById("ddReferences")
  var  ttl;
  // Loop through all rows and columns of the table and popup alert with the value
  // /content of each cell.
  for ( var i = 0; row = refTab.rows[i]; i++ ) {
     row = refTab.rows[i];
     for ( var j = 0; col = row.cells[j]; j++ ) {
        alert(col.firstChild.nodeValue);
     }
  }

What is the correct call I should be putting in to the alert() call to display the contents of each cell of my HTML table? This should be in JS...can't use jQuery.

解决方案

function GetCellValues() {
    var table = document.getElementById('mytable');
    for (var r = 0, n = table.rows.length; r < n; r++) {
        for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
            alert(table.rows[r].cells[c].innerHTML);
        }
    }
}

这篇关于从JavaScript表格中获取价值...不是jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆