【问题标题】:add rows and columns to atable and display the row and column no向表格添加行和列并显示行和列编号
【发布时间】:2017-06-21 16:44:06
【问题描述】:

我需要有关动态添加行和列并显示行和列编号的 javascript 代码帮助。动态的。

 <!DOCTYPE html>
  <html>
  <head>
   <style>
    table, td {
   border:1px solid black;
  }
 </style>
 </head>
 <body>

<p>Click on each tr element to alert its index position in the table:</p>

   <table>
    <tr onclick="myFunction(this)">
   <td>Click to show rowIndex</td>
   </tr>
   <tr onclick="myFunction(this)">
   <td>Click to show rowIndex</td>
   </tr>
   <tr onclick="myFunction(this)">
  <td>Click to show rowIndex</td>
 </tr>
</table>

<script>
function myFunction(x) {
alert("Row index is: " + x.rowIndex);
}
</script>

</body>

这是我尝试过的,但我不确定表的行和列是否是动态生成的,并且列是否没有重新生成和显示。 请帮忙。

【问题讨论】:

  • 贴出你试过的代码。

标签: javascript


【解决方案1】:

首先不要手动将事件处理程序添加到每个 td/tr 元素,使用 JQuery 将其添加到所有 td 元素:

$('td').click(function(e) {
    console.log($(this).parent().index(), $(this).index());
});

在事件处理程序中,$(this).parent().index() 和 $(this).index() 将分别为您提供行号和列号。

我建议查看用于动态添加行/列的 JQuery append() 或 clone() 函数,但您并没有真正具体说明您在这方面要做什么。

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2011-05-19
    • 2011-05-14
    • 1970-01-01
    相关资源
    最近更新 更多