Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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 如何将变量ID分配给表、行和列_Javascript_Html_Html Table - Fatal编程技术网

Javascript 如何将变量ID分配给表、行和列

Javascript 如何将变量ID分配给表、行和列,javascript,html,html-table,Javascript,Html,Html Table,如果要使用此方法创建表,如何为行、列分配ID。知道ID是变量,例如数组长度 function addRow(tableID, text) { // Get a reference to the table var tableRef = document.getElementById(tableID); // Insert a row in the table var newRow = tableRef.insertRow(); // Insert a cell in th

如果要使用此方法创建表,如何为行、列分配ID。知道ID是变量,例如数组长度

function addRow(tableID, text) {
  // Get a reference to the table
  var tableRef = document.getElementById(tableID);

  // Insert a row in the table
  var newRow = tableRef.insertRow();

  // Insert a cell in the row
  var newCell = newRow.insertCell();

  // Append a text node to the cell
  var newText = document.createTextNode(text);
  newCell.appendChild(newText);
}

// Call addRow(text) with the ID of a table
addRow('TableA', 'Brand new row');
addRow('TableA', 'Another new row');

假设您要添加
k
行。简单地说,在
k
迭代的循环中创建行

var tableRef = document.getElementById(tableID);

//to add k rows
for (x=0; x<k; x++)
{

  // Insert a row in the table
  var newRow = tableRef.insertRow(x);
  newRow.setAttribute("id","row"+x);

  // Insert a cell in the row
  var newCell = newRow.insertCell(0);

  // Insert another cell in the row
  var newCell2 = newRow.insertCell(1);

  //insert HTML text in the cells
  newCell.innerHTML="text1";
  newCell2.innerHTML="text2";
} //end for

}//end function
var tableRef=document.getElementById(tableID);
//添加k行的步骤

对于(x=0;x这是我按照您的逻辑制作的一个示例,并向行和列添加ID,实际上并不基于任何数组长度,但可以带您到您想要的地方:我不认为这是我想要的。我希望稍后使用getElementByid。那么我将如何使用您的代码?如果您正在创建元素,请在创建时存储元素。为什么如果您已经可以引用元素,那么麻烦执行
getElementById
?根据您的评论,这是一个–告诉我们您计划对表执行什么操作,而不是询问如何分配ID。我需要的是创建包含按钮的列的行。我想在按钮上附加一个侦听器,以便在单击该按钮时,它会被删除rforms该行中的函数。