Jquery - 如何选择最接近TD的Dom元素

时间:2015-03-01 14:38:36

标签: jquery jquery-selectors

我需要使用选择器来获取CSS格式不佳的网页的HTML内容。

如何通过“我需要此处”获取文字内容?

<tbody>
    <tr class="nth">
        <td class="title">Address</td>
        <td>I need this here</td>
    </tr>
</tbody>

有人可以提供一些线索吗?

使用这个我得到了“地址”:

$('tbody tr.nth td').html()

4 个答案:

答案 0 :(得分:1)

这应该有助于找到第二个<td>孩子。

$('tbody tr.nth td:eq(1)').html()

参考: :eq() Selector

答案 1 :(得分:1)

您可以使用.eq()

  

将匹配元素集合减少到指定索引处的元素。

$('tbody tr.nth td').eq(1).html()

OR

$('tbody tr.nth td.title').next('td').html()

答案 2 :(得分:1)

您可以使用:last选择器选择 last 匹配的TD:

&#13;
&#13;
document.write($('tbody tr.nth td:last').html())
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
    <tr class="nth">
        <td class="title">Address</td>
        <td>I need this here</td>
    </tr>
</tbody>
</table>
<hr>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

尝试

$(".nth :not(.title)").html();

console.log($(".nth :not(.title)").html())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js">
</script>
<table>
<tbody>
    <tr class="nth">
        <td class="title">Address</td>
        <td>I need this here</td>
    </tr>
</tbody>
  </table>