【问题标题】:Get row and column index of named cell获取命名单元格的行和列索引
【发布时间】:2016-08-28 20:40:36
【问题描述】:

我试图解决 HTML 表格的小问题。我有一个表,其中每个 TD 都有其唯一的名称。我试图获取如此命名的单元格的行和列索引,但我仅使用 cellIndex 成功,

var rownr=document.getElementById("cell122").cellIndex;

其中 cell122 是对应 TD 的 ID,但 rowIndex 返回“未定义”值。 可能吗?

【问题讨论】:

  • 我假设#cell122 不是TD ...?或者您的 ID 不是唯一的。等待! TDs 没有 rowIndex,只有 TRs 有。你必须从parentNodeTD 中获得rowIndex
  • 我有一个 PHP 例程,它为表的每个 TD 生成唯一 ID。我认为我应该能够点击 TD 找到表格的哪一列和哪一列。感谢您的意见。
  • TDs 没有rowIndex,只有TRs 有”不是意见,而是事实。您可以通过检查单元格的.parentElement.rowIndex.parentNode.rowIndex 来获取包含单击单元格的行的rowIndex

标签: javascript html html-table row col


【解决方案1】:

对于 rowIndex 使用 tr:

https://jsfiddle.net/co8pxq2f/1/

   var row = document.querySelectorAll("table#test tr");
   for (var i = 0; i < row.length;i++) {
       row[i].textContent = "RowIndex is "+ row[i].rowIndex;
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2011-10-16
    相关资源
    最近更新 更多