Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用insertRow()为TableRow指定ID?_Javascript - Fatal编程技术网

Javascript 如何使用insertRow()为TableRow指定ID?

Javascript 如何使用insertRow()为TableRow指定ID?,javascript,Javascript,我正在使用这样的代码。如何为tr(TableRow)提供Id。帮帮我。因为使用标准Javascript,所以必须使用函数setAttribute(“align”,“center”,0) 请尝试以下操作: 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' a

我正在使用这样的代码。如何为tr(TableRow)提供Id。帮帮我。

因为使用标准Javascript,所以必须使用函数
setAttribute(“align”,“center”,0)

请尝试以下操作:

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; 

tr.id=“some_id”。这将起作用。

首先创建一个变量,如var id=0

然后

tr.id=id;

id++是否有原因
tr.id='value'不起作用?当我使用
document.createElement()
时,我总是这样做,它工作得很好-它与
insertRow()
一起工作不是吗?按照您的方式,属性必须存在,否则浏览器会返回错误(错误:对未定义的值进行操作)。只有浏览器的程序员知道它为什么不起作用。那么,为什么我可以通过直接命名HTMLElement对象来为它们添加自定义属性和方法呢?我一直在玩这个,我不能让它打破任何地方…不要问我这个。当我尝试向iframe添加'src'属性时,我经常遇到同样的问题。不管是什么浏览器(比如FF)都没有接受它。我很困惑。但我可能会开始使用setAttribute。我从来没有遇到过这个问题,试试@reporters解决方案——你也有同样的问题吗?
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; 
tr.setAttribute("id", "myIdNameforThisRow", 0);