获取表属性的最后一行

Lan*_*ndY 5 javascript dom

var table = document.getElementById(table1);
var lastRow = table.rows.length;

var country = lastRow.getElementById('Country');
country.setAttribute('class', "Country"+lastRow);   
Run Code Online (Sandbox Code Playgroud)

我很新,我想知道这是否可能?

代替

var Country= document.getElementById('Country');
Run Code Online (Sandbox Code Playgroud)

因为我有其他id = Country因为我正在添加新行..

编辑

我知道身份证是独特的.但是我克隆了最后一行,所以ID当然都是一样的.

mwc*_*wcz 9

可以从rows属性访问表的最后一行.

var lastRow = table.rows[ table.rows.length - 1 ];
Run Code Online (Sandbox Code Playgroud)

不过,我认为对于你究竟要求的内容存在一些困惑.


Jar*_*Par 1

HTML 文档中的 ID 是唯一的。因此,不需要执行子查询来查找元素。相反,总是使用document.getElementById('Country')