gpt4 book ai didi

javascript - 单击时获取 中 的值

转载 作者:行者123 更新时间:2023-11-28 15:53:20 25 4
gpt4 key购买 nike

假设我有一张 table

<table id="data-table" id="test">
<thead>
<tr>
<th>value1</th>
<th>value2</th>
<th>value3</th>
<th>value4</th>
</tr>
</thead>
<tbody>
<tr>
<td id="class1"><%= value.valueOne() %></td>
<td id="class2"><%= value.valueTwo() %></td>
<td id="class3"><%= value.valueThree() %></td>
<td id="class4"><%= value.valueFour() %></td>
</tr>
<%
}
%>
</tbody>
</table>

如何获得<td> a 的值 <tr>当特定 <tr> 时作为单独的变量被点击了?我尝试使用以下代码片段,但没有帮助

$(this).closest('td').closest('tr').html() 

最佳答案

我认为您是说您想要获取单击的任何 tr 中的 td 元素的每个值。

您可以使用 findmapget 来获取值数组:

$('#test tbody tr').click(function() {
var values = $(this).find('td').map(function() {
return $(this).text();
}).get();
});

jsFiddle (注意,我已经删除了 table 元素上的一个 id 值:您不能有两个...)

关于javascript - 单击时获取 <td> 中 <tr> 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19816185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com