Javascript 无法使用prev和prevAll遍历到上一列
我需要帮助来使用prev,prevAll从一个列中获取文本。JSIDLE链接: 如果我的起点是超链接中的,则程序将无法正确遍历到上一列。但是,如果起点在a中,我可以毫无问题地遍历到上一列。我需要能够有一个超链接内的起点 第一个不行Javascript 无法使用prev和prevAll遍历到上一列,javascript,jquery,Javascript,Jquery,我需要帮助来使用prev,prevAll从一个列中获取文本。JSIDLE链接: 如果我的起点是超链接中的,则程序将无法正确遍历到上一列。但是,如果起点在a中,我可以毫无问题地遍历到上一列。我需要能够有一个超链接内的起点 第一个不行 $('.a1').prevAll('td:first').css('backgroundColor', 'yellow'); alert($('.a1').prev('td:first').text()) 第二个有效 $('.td1').prevAll('td:
$('.a1').prevAll('td:first').css('backgroundColor', 'yellow');
alert($('.a1').prev('td:first').text())
第二个有效
$('.td1').prevAll('td:first').css('backgroundColor', 'orange'); //This works!
alert($('.td1').prev('td:first').text())
这是表格:
<table>
<col width="180" />
<col width="180" />
<col width="180" />
<tr class="usetr">
<td>Row 1, Col 1</td>
<td>Row 1, Col 2</td>
<td>Row 1, Col 3</td>
</tr>
<tr>
<td>Row 2, Col 1</td>
<td>Row 2, Col 2</td>
<td>Row 2, Col 3</td>
</tr>
<tr>
<td class="td3">Row 3, Col 1</td>
<td class="td4">Row 3, Col 2</td>
<td class="td5">Row 3, Col 3</td>
</tr>
<tr>
<td><a class="a2" href="#">Row 4, Col 1 </a>
</td>
<td class="td1"><a class="a1" href="#">Row 4, Col 2 </a>
</td>
<td class="td2"><a class="a3" href="#">Row 4, Col 3 </a>
</td>
</tr>
</table>
试过这个,效果很好!!这是JSFIDLE更新: 但我不知道为什么。有人能解释吗
$('.a1').closest(":has(td)").find('.a2').css('backgroundColor','yellow');
alert($('.a1').closest(":has(td)").find('.a2').text())
这里有另一个片段也会有用!!多亏了乔治
$('.a1').closest('td').prev().css('backgroundColor', 'yellow');
alert($('.a1').closest('td').prev().text())
您是否尝试过$'.a1'。最近的'td'。上一个…?是的!它也有用!!!我不明白为什么要有最近的?