繁体   English   中英

如何设置新创建的表行或单元格的ID?

[英]How do I set the ID of a newly created table Row or cell?

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()">&times;</button>';   

如何设置刚创建的行或表的ID?

您可以只设置新dom元素引用的id属性

row.id='newid';
cell1.id='cellid1'
....

在底部表格中添加行并设置动态ID。 简洁的方法:

var newTr = $("<tr id="+e+"></tr>"); 
$("#TableID").append(NewTr);

我的例子:

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');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM