Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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更改新行的html元素的名称_Javascript_Html - Fatal编程技术网

无法通过JAVASCRIPT更改新行的html元素的名称

无法通过JAVASCRIPT更改新行的html元素的名称,javascript,html,Javascript,Html,嘿,伙计们需要帮忙, 我使用addROW函数向表中添加新行,但我想更改每一新行的html元素的名称。 代码 函数addRow(tableID){ var table=document.getElementById(tableID); var rowCount=table.rows.length; var row=table.insertRow(rowCount); var colCount=table.rows[0].cells.length; 对于(var i=0;i您是否考虑过使用setAt

嘿,伙计们需要帮忙, 我使用addROW函数向表中添加新行,但我想更改每一新行的html元素的名称。 代码

函数addRow(tableID){
var table=document.getElementById(tableID);
var rowCount=table.rows.length;
var row=table.insertRow(rowCount);
var colCount=table.rows[0].cells.length;

对于(var i=0;i您是否考虑过使用setAttribute函数。请尝试以下方法:

.....childNodes[0].setAttribute("name", "yourName");
这可能需要您进行更多的搜索,以使其适用于您的示例,但这只是一个开始


需要注意的一点是,子节点将返回所有子节点可能性的集合,这些子节点可能是标记、文本节点或注释,因此更好的选择是使用getElementsByTagName()。

看看我添加到这个答案中的示例。我想它正在设置文本元素名称


您是否收到任何错误?此函数之后会发生什么情况?该函数工作正常,但当我检查新html元素的名称时,它们保持不变。是否进入案例?您是否发出警告?希望您按照上述注释进行检查。
使用setAttributes('attrName','value'))
这将起作用。setAttribute部分不起作用。这是我尝试过的案例“text”:newcell.childNodes[0]。value=“”;newcell.childNodes[0]。setAttribute('name','newrating'));break;setAttribute选项似乎也不起作用我尝试了这种情况“text”:newcell.childNodes[0]。value=“”;newcell.childNodes[0]。setAttribute('name','newrating'));break;如果有帮助,setAttribute中的参数应该用双引号括起来?您可以提醒该位置已有的元素名称吗?如果打印出childnodes中的选项,则可以查看可以更改的选项。您可以尝试此操作。var elementVar=newcell.childnodes[0].getElementsById(“td”)[0];elementVar.setAttribute(“name”、“yourName”);这将行中每个单元格的名称设置为相同的名称,我打算将它们设置为不同的名称
.....childNodes[0].setAttribute("name", "yourName");