javascript 如何设置新创建的表格行或单元格的 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21921859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:00:17 来源:igfitidea点击:
How do I set the ID of a newly created table Row or cell?
提问by user2179936
var table = document.getElementById("table-body");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = ingredient_name;
cell2.innerHTML = ingredient_amount;
cell3.innerHTML = ingredient_unit;
cell4.innerHTML = '<button type="button" class="close" aria-.hidden="true"onClick="$(this).closest(\'tr\').remove()">×</button>';
How would I set the ID of a row or a table that I've just created?
我将如何设置我刚刚创建的行或表的 ID?
回答by Arun P Johny
You can just set the id
property of the new dom element reference
您可以只设置id
新 dom 元素引用的属性
row.id='newid';
cell1.id='cellid1'
....
回答by zokaee hamid
add row in bottom Table and set dynamic ID. nice and neat way:
在底部表中添加行并设置动态 ID。漂亮整洁的方式:
var newTr = $("<tr id="+e+"></tr>");
$("#TableID").append(NewTr);
my example :
我的例子:
function showimage(e) {
debugger;
for (var i = 0; i < e.length; i++) {
var fd = new FormData();
fd.append("ID", e[i].ID);
$.ajax({
url: "/AdminPanel/Pictures/ShowImage2",
type: 'POST',
contentType: false,
processData: false,
data: fd
}).then(function (e) {
var newTr = $("<tr id="+e+"></tr>");
var ImageTd = $('<td></td>');
var DeleteLinkTd = $('<td></td>');
var img = document.createElement("IMG");
img.src = "/AdminPanel/Pictures/ShowImage/"+e;
img.width = "100";
img.alt = "#" + e;
ImageTd.append(img);
var aTag = document.createElement('a');
aTag.setAttribute('onClick', "Delete("+e+");");
aTag.innerHTML = "Delete";
DeleteLinkTd.append(aTag);
newTr.append(ImageTd);
newTr.append(DeleteLinkTd);
$('#PicturesTable').append(newTr);
});}
$('#DivBulkUpload').dialog('close');
}