Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用javascript检查给定html表有多少列?_Javascript_Html Table - Fatal编程技术网

如何用javascript检查给定html表有多少列?

如何用javascript检查给定html表有多少列?,javascript,html-table,Javascript,Html Table,这就是最大colspan的方式,例如: <table> <tr> <td> 1 </td> <td> 2 </td> <td> 3 </td> </tr> <tr> <td> 1 </td> <td> 2 </td> </tr> <tr> <td> 1 <

这就是最大colspan的方式,例如:

<table>
 <tr>
  <td> 1 </td>
  <td> 2 </td>
  <td> 3 </td>
 </tr>
 <tr>
  <td> 1 </td>
  <td> 2 </td>
 </tr>
 <tr>
  <td> 1 </td>
 </tr>
</table>
应该给3

使用getElementsByTagName查找每个 对于每个,找到每个相似的 对元素进行计数,然后遍历并为每个元素添加colspan-1,每个元素都有一个colspan属性 在所有行上保持最大计数。 使用getElementsByTagName查找每个 对于每个,找到每个相似的 对元素进行计数,然后遍历并为每个元素添加colspan-1,每个元素都有一个colspan属性 在所有行上保持最大计数。 有关jQuery版本,请参阅。它统计每个tr中td元素的数量,并每次检查是否遇到更多的td元素。max变量存储当前找到的最大列数:

var max = 0;
$("tr").each(function() {
   var count = $(this).find("td").length;
    if(count > max) max = count;
});
alert(max);
有关jQuery版本,请参阅。它统计每个tr中td元素的数量,并每次检查是否遇到更多的td元素。max变量存储当前找到的最大列数:

var max = 0;
$("tr").each(function() {
   var count = $(this).find("td").length;
    if(count > max) max = count;
});
alert(max);
每个表都有一个行数组,每行都有一个单元格数组。那就是找到最大值的问题了

function calculateCells(){
    var table = document.getElementById("table");
    var max = 0;
    for(var i=0;i<table.rows.length;i++) {
        if(max < table.rows[i].cells.length)
            max = table.rows[i].cells.length;
    }
    return max;
}
每个表都有一个行数组,每行都有一个单元格数组。那就是找到最大值的问题了

function calculateCells(){
    var table = document.getElementById("table");
    var max = 0;
    for(var i=0;i<table.rows.length;i++) {
        if(max < table.rows[i].cells.length)
            max = table.rows[i].cells.length;
    }
    return max;
}
看看这个例子。按照您的要求,它是纯JavaScript的:

var table = document.getElementById("myTable");
var max = 0;

for (var i = 0, iLen = table.rows.length; i < iLen; i++) {
  var temp = 0;
  var cells = table.rows[i].cells;

  for (var j = 0, jLen = cells.length; j < jLen; j++) {
    // This is very important. If you just take length you'll get the
    // the wrong answer when colspan exists
    temp += cells[j].colSpan;
  }

  if (temp > max) {
    max = temp;
  }
}

alert(max);
看看这个例子。按照您的要求,它是纯JavaScript的:

var table = document.getElementById("myTable");
var max = 0;

for (var i = 0, iLen = table.rows.length; i < iLen; i++) {
  var temp = 0;
  var cells = table.rows[i].cells;

  for (var j = 0, jLen = cells.length; j < jLen; j++) {
    // This is very important. If you just take length you'll get the
    // the wrong answer when colspan exists
    temp += cells[j].colSpan;
  }

  if (temp > max) {
    max = temp;
  }
}

alert(max);

普通JS或使用特定的框架jQuery/mootools/etc?难道每个表行的列数不应该相同吗?或者在列数最少的行的单元格上至少有一个colspan?我更喜欢纯JS。对不起,我没说出来。@Jan我就是这么想的。如果这是询问者的网站,他可能应该修复一些问题,使所有行都具有相同的权限。如果不是,当然,这是正确的方法。我还发现colspan='100%'设置标记do maximum available colspan.->这里是纯JS还是使用特定的框架jQuery/mootools/etc?每个表行的列数不应该相同吗?或者在列数最少的行的单元格上至少有一个colspan?我更喜欢纯JS。对不起,我没说出来。@Jan我就是这么想的。如果这是询问者的网站,他可能应该修复一些问题,使所有行都具有相同的权限。如果不是,当然,这是正确的方法。我还发现colspan='100%'设置标记do maximum available colspan.->这里@Guffa是的,我讨厌这些:-你是对的;那会使它。。。奇怪。@Guffa是的,我讨厌这些:-你是对的;那会使它。。。奇怪的