使用 javascript 获取 TableHeader 的 cellIndex
Get cellIndex of TableHeader with javascript
情况:我完全用javascript创建了一个html table header,现在我想添加一个删除列功能,它将单元格索引作为争论。但是,每次我尝试获取所创建元素的单元格索引时,获得的索引都是-1。一旦我通过标签名称获取元素,获取 header.
的相应单元格索引就不再有任何问题
非常感谢任何帮助。
let headerFragment = document.createDocumentFragment();
//Append counter at the beginning of each row
let number_th = document.createElement("th")
let number_txt = document.createTextNode('#');
number_th.appendChild(number_txt);
headerFragment.appendChild(number_th);
//Append active checkbox
let active_th = document.createElement("th")
let active_txt = document.createTextNode('Active');
active_th.appendChild(active_txt);
headerFragment.appendChild(active_th);
//Append all other data fields
if (headersArray){
headersArray.forEach((header, index) => {
let th = document.createElement("th");
//Create text identifier
let txt = document.createTextNode(headersArray[index]);
//Create delete column button
let delete_column = document.createElement("p");
delete_column.innerHTML = "Delete";
delete_column.onclick = function() {
//Delete column - here is the problem
alert(th.cellIndex) //alerts -1
deleteColumn(th.cellIndex);
}
th.appendChild(txt);
th.appendChild(delete_column);
headerFragment.appendChild(th);
});
//Final append
tableHeader.appendChild(headerFragment);
}
如果我运行这个函数所有索引都被正确检索
function myFunction() {
var x = document.getElementsByTagName("th");
var txt = "";
var i;
for (i = 0; i < x.length; i++) {
txt = txt + "The index of Cell "+(i+1)+" is: "+x[i].cellIndex+"<br>";
}
alert(txt);
}
当单元格不属于 tr
时,docs 声明 cellIndex
returns -1,所以我的猜测是这种情况.
我建议您为“th”元素动态分配一个 ID,然后使用该 ID 将其删除。
声明一个变量
let i = 0;
然后
let th = document.createElement("th");
th.id = i++;
在你删除的可能是
delete_column.onclick = function() {
let elementToDelete = document.getElementById(th.id);
elementToDelete.parentNode.removeChild(elementToDelete);
}
情况:我完全用javascript创建了一个html table header,现在我想添加一个删除列功能,它将单元格索引作为争论。但是,每次我尝试获取所创建元素的单元格索引时,获得的索引都是-1。一旦我通过标签名称获取元素,获取 header.
的相应单元格索引就不再有任何问题非常感谢任何帮助。
let headerFragment = document.createDocumentFragment();
//Append counter at the beginning of each row
let number_th = document.createElement("th")
let number_txt = document.createTextNode('#');
number_th.appendChild(number_txt);
headerFragment.appendChild(number_th);
//Append active checkbox
let active_th = document.createElement("th")
let active_txt = document.createTextNode('Active');
active_th.appendChild(active_txt);
headerFragment.appendChild(active_th);
//Append all other data fields
if (headersArray){
headersArray.forEach((header, index) => {
let th = document.createElement("th");
//Create text identifier
let txt = document.createTextNode(headersArray[index]);
//Create delete column button
let delete_column = document.createElement("p");
delete_column.innerHTML = "Delete";
delete_column.onclick = function() {
//Delete column - here is the problem
alert(th.cellIndex) //alerts -1
deleteColumn(th.cellIndex);
}
th.appendChild(txt);
th.appendChild(delete_column);
headerFragment.appendChild(th);
});
//Final append
tableHeader.appendChild(headerFragment);
}
如果我运行这个函数所有索引都被正确检索
function myFunction() {
var x = document.getElementsByTagName("th");
var txt = "";
var i;
for (i = 0; i < x.length; i++) {
txt = txt + "The index of Cell "+(i+1)+" is: "+x[i].cellIndex+"<br>";
}
alert(txt);
}
当单元格不属于 tr
时,docs 声明 cellIndex
returns -1,所以我的猜测是这种情况.
我建议您为“th”元素动态分配一个 ID,然后使用该 ID 将其删除。
声明一个变量
let i = 0;
然后
let th = document.createElement("th");
th.id = i++;
在你删除的可能是
delete_column.onclick = function() {
let elementToDelete = document.getElementById(th.id);
elementToDelete.parentNode.removeChild(elementToDelete);
}