Javascript 如何使用 insertRow() 为 TableRow 提供 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7160897/
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
How to give ID for TableRow using insertRow()?
提问by Aravinth
var table = document.getElementById("table1");
var tr = table.insertRow();
var td = tr.insertCell();
td.innerHTML= document.getElementById('txt1').value;
i am using code like this. how to give Id for tr(TableRow). help me.
我正在使用这样的代码。如何为 tr(TableRow) 提供 Id。帮我。
回答by AmGates
Just give tr.id = "some_id". This will work.
只要给 tr.id = "some_id"。这将起作用。
回答by Reporter
because you use standard Javascript you have to use the function setAttribute("align", "center", 0);
.
因为您使用标准 Javascript,所以您必须使用该功能setAttribute("align", "center", 0);
。
Try following:
尝试以下操作:
tr.setAttribute("id", "myIdNameforThisRow", 0);
回答by DaveRandom
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;
回答by aswathy
create a variable first like var id=0;
首先创建一个变量,如 var id=0;
then
然后
tr.id=id;
id++;
use this code
使用此代码