[英]How can I select a specific column in a row using jQuery and JavaScript?
I am very new to jQuery and JavaScript. 我是jQuery和JavaScript的新手。 I have a small question. 我有一个小问题。 Let's say i have a HTML table like the following 假设我有一个如下所示的HTML表格
<Table id="mytable">
<tr id="element">
<td>value</td>
<td>text</td>
</tr>
</Table>
In the above example i know the row id and i want to change the value of the second column of the row with that particular id. 在上面的例子中,我知道行id,我想更改具有该特定id的行的第二列的值。
I need a result something like the following: 我需要一个如下结果:
<Table id="mytable">
<tr id="element">
<td>value</td>
<td>ChangedText</td>
</tr>
</Table>
So my question is: how can I select the 2 nd column of the first row with a given id in order to change the value? 所以我的问题是:我如何选择具有给定id的第一行的第二列才能更改值?
$("#element td:nth-child(2)").text('ChangedText');
这是一个例子 。
something like 就像是
$('#mytable tr:eq(0) td:eq(1)').text('ChangedText');
will select the first row, second column (0 based) of the given element (TABLE). 将选择给定元素(TABLE)的第一行,第二列(基于0)。 In your case, since you know the row id : 在您的情况下,因为您知道行ID:
$('#mytable #element td:eq(1)').text('ChangedText');
or simply 或者干脆
$('#element td:eq(1)').text('ChangedText');
Gert's code is how I would have implemented what you are asking so I won't repost it. Gert的代码是我如何实现你所要求的,所以我不会重新发布它。 However since you are new to jquery/javascript, you might like this tool I use to make sure my selectors are working http://www.woods.iki.fi/interactive-jquery-tester.html . 但是,由于您不熟悉jquery / javascript,您可能会喜欢这个工具,以确保我的选择器正在工作http://www.woods.iki.fi/interactive-jquery-tester.html 。
Cheers, Joe 干杯,乔
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.