简体   繁体   English

获取表属性的最后一行

[英]Get last row of table attribute

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

var country = lastRow.getElementById('Country');
country.setAttribute('class', "Country"+lastRow);   

Im pretty new and I was wondering if this is possible? 我很新,我想知道这是否可能?

Instead of 代替

var Country= document.getElementById('Country');

As I have other id=Country because I am adding new rows.. 因为我有其他id = Country因为我正在添加新行..

EDIT 编辑

I know ID are unique. 我知道身份证是独特的。 But I am cloning the last row so of course the IDs will all be the same. 但是我克隆了最后一行,所以ID当然都是一样的。

The last row of a table can be accessed from the rows property. 可以从rows属性访问表的最后一行。

var lastRow = table.rows[ table.rows.length - 1 ];

However I think there is some confusion as to what exactly you're asking for. 不过,我认为对于你究竟要求的内容存在一些困惑。

I think the best way to do this is : 我认为最好的方法是:

var lastTr = document.getElementById('tabRecapCommande').lastElementChild.lastElementChild;

with document.getElementById('tabRecapCommande') returning the <table> document.getElementById('tabRecapCommande').lastElementChild . with document.getElementById('tabRecapCommande')返回<table> document.getElementById('tabRecapCommande').lastElementChild returning the <tbody> document.getElementById('tabRecapCommande').lastElementChild.lastElementChild the last <tr> of the <tbody> 返回<tbody> document.getElementById('tabRecapCommande').lastElementChild.lastElementChild <tbody>的最后一个<tr> <tbody>

if you console.log(lasTr) you will see your last <tr> element :) 如果你console.log(lasTr)你会看到你的最后一个<tr>元素:)

Here is how to implicitly get the last cell. 以下是如何隐式获取最后一个单元格。 Note that you can reference a particular object in the rows or cells array using the square bracket notation, and that due to arrays indexing starting at 0 you must subtract 1 from the length to use it as a valid last index. 请注意,您可以使用方括号表示法引用行或单元格数组中的特定对象,并且由于数组索引从0开始,您必须从长度中减去1以将其用作有效的最后一个索引。

This example show cases some of the things you can do: 此示例显示了您可以执行的一些操作:

<!DOCTYPE html>
<html>

<head>
<title>Table test</title>

<script language="javascript" type="text/javascript">

function init() {

var table = document.getElementById("table1");

var lastRowIndex = table.rows.length-1;

var lastCellIndex = table.rows[lastRowIndex].cells.length-1;

alert( table.rows[lastRowIndex].cells[lastCellIndex].innerHTML ); // alerts the cell's containing HTML, or 9

var lastCell = table.rows[lastRowIndex].cells[lastCellIndex]; // contains a reference to the last cell

}



window.onload = init;
</script>

</head>

<body>


<table id="table1">
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
<tr><td>7</td><td>8</td><td>9</td></tr>
</table>


</body>

</html>

Suppose you had an arbitrary table with 8 columns and 4 rows as in the following example, and wanted cell in column 5 and row 3 to get that cell you'd want to grab a reference to the table (such as through getElementById ) and then select for the right row and column through rows and cells making sure to subtract 1 due to arrays indexing starting at 0. See this example: 假设您有一个包含8列和4行的任意表,如下例所示,并希望第5列和第3行中的单元格获取您想要获取对表的引用的单元格(例如通过getElementById )然后通过rowscells选择正确的行和列,确保减去1,因为数组索引从0开始。请参阅此示例:

<!DOCTYPE html>
<html>

<head>
<title>Table test</title>

<script language="javascript" type="text/javascript">

function init() {

var table = document.getElementById("table1");

var column5Row3 = table.rows[2].cells[4]; // contains a reference to the cell that is in the 3rd row, and 5th column

alert( column5Row3.innerHTML ); // alerts that cell's innerHTML (or Y)

}



window.onload = init;
</script>

</head>

<body>


<table id="table1">
<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>2</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td></tr>
<tr><td>3</td><td>x</td><td>x</td><td>x</td><td>Y</td><td>x</td><td>x</td><td>x</td></tr>
<tr><td>4</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td><td>x</td></tr>
</table>


</body>

</html>

An ID in an HTML document is unique. HTML文档中的ID是唯一的。 Hence there is no need to do sub-queries in order to find elements. 因此,不需要进行子查询以便找到元素。 Instead always use document.getElementById('Country') 而是始终使用document.getElementById('Country')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM