js 通过td的id值 如何拿到tr的id值?(动态获取)

JavaScript014

js 通过td的id值 如何拿到tr的id值?(动态获取),第1张

错误出在document.getElementById(dd)

因为页面上id为2的出现了多个,而这样获取只会取到第一个,所以tr始终为相应的第1个.

按照你的写法,用dd来保存tr的行数比较好,即i值。

然后通过document.all.table1.rows(i).id来获取tr的id值。

js中获取tr的首个td的方法如下:

比如有如下表格table:

<tr id="value_12">

<td>firstanme_1</td>

<td>lastname_1</td>

</tr>

<tr id="value_14">

<td>firstanme_2</td>

<td>lastname_2</td>

</tr>

用js获取的方法定义:

$('#table_id tr').each(function() {

var firstName = $("td", tr)[0].val()

var id = $(tr).attr('id').split("_")[1]

})