gpt4 book ai didi

jQuery - 查找相对于单击的元素的值

转载 作者:行者123 更新时间:2023-12-01 07:02:14 26 4
gpt4 key购买 nike

我正在尝试获取与单击的链接相关的表格单元格的文本值。单击类“三”的任何单元格中的链接,并获取该特定行的名为类“一”的单元格的文本。

<tr>
<td class='one'><a href="#">Text to get</a></td>
<td class='two'>meh</td>
<td class='three'><a href="#">Click this to get the text in the first cell of this row</a></td>
</tr>
<tr>
<td class='one'>Different text to get</td>
<td class='two'>blah</td>
<td class='three'><a href="#">Click this to get the text in the first cell of this row</a></td>
</tr>

我可以通过以下方式获取单击元素的文本:

console.log($(this).text());

但是如何获取该行中第一个单元格的文本?

我认为,举个例子,它会是这样的:

console.log($(this).prev().prev().text());

但这不正确(返回“空字符串”)。有什么建议吗?

最佳答案

尝试:

$(this).closest('td').siblings('.one').text();

关于jQuery - 查找相对于单击的元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2239994/

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