var table = document.getElementById("table1");
var tr = table.insertRow();
var td = tr.insertCell();
td.innerHTML= document.getElementById('txt1').value;
我正在使用類似這樣的代碼。如何給tr(TableRow)提供Id。幫我。如何使用insertRow()爲TableRow提供ID?
var table = document.getElementById("table1");
var tr = table.insertRow();
var td = tr.insertCell();
td.innerHTML= document.getElementById('txt1').value;
我正在使用類似這樣的代碼。如何給tr(TableRow)提供Id。幫我。如何使用insertRow()爲TableRow提供ID?
只要給 tr.id = 「SOME_ID」。這將工作。
var table = document.getElementById("table1");
var tr = table.insertRow();
tr.id = 'id_for_this_row'; // Just assign a value to the new row's 'id' attribute
var td = tr.insertCell();
td.innerHTML= document.getElementById('txt1').value;
因爲你使用標準的Javascript,你必須使用函數'setAttribute(「align」,「center」,0);'。
嘗試以下操作:
tr.setAttribute("id", "myIdNameforThisRow", 0);
是否有原因'tr.id ='價值';'不會工作?這是我總是這樣做,當我'document.createElement()',它工作得很好 - 不會與'insertRow()'工作嗎? – DaveRandom
以您的方式,該屬性必須存在,否則瀏覽器將返回一個錯誤(錯誤:操作未定義的值)。只有瀏覽器的程序員知道爲什麼它不起作用。 – reporter
爲什麼我可以通過直接命名它們將自定義屬性和方法添加到HTMLElement對象,然後呢?我剛剛一直在玩這個,我不能把它打破任何地方... – DaveRandom
首先創建一個變量,如var id = 0;
然後
tr.id=id;
id++;
使用此代碼
但它不能在Firefox – Aravinth
工作,我從未有過這樣的問題,嘗試@reporters解決方案 - 你有同樣的問題? – DaveRandom
這將工作在Firefox也沒有任何問題 – AmGates