Javascript 如何使用insertRow()为TableRow指定ID?
我正在使用这样的代码。如何为tr(TableRow)提供Id。帮帮我。因为使用标准Javascript,所以必须使用函数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
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);