2014-02-20 49 views
0
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?如何設置新創建的表格行或單元格的ID?

+0

沒有設置'row.id = 「行號」;'不行? – squid314

+0

我不斷收到函數沒有定義的錯誤 – user2179936

回答

7

只需將新的DOM元素參考

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

純粹,簡單,乾淨:) – java

+0

我與tr.id ='the id'戰鬥;當添加一行時,但這工作,很簡單 – user1946891

2

添加行中底表的id屬性和設置動態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'); 
} 
相關問題