如何使用 jQuery 从多个 <td> 中获取 <td> 之一的值?

How to get value of one of <td> from several <td> using jQuery?

我得到了下一个代码:

<tr>
    <td id='type'> Type name </td>
    <td id='number'> 102030 </td>
    <td id='software'> 1.0-Alpha </td>
</tr>

我需要获取 ID 为 'number' 的 td 的值。 我现在使用下一个代码并且它有效:

$('tbody tr').click(function() {
    var tdNumber = $(this).find('td:nth-child(2)').html();
});

但我不想按子元素查找,而是按id。我怎样才能做到这一点?

要定位 ID,只需使用 #

$('#number').html();

这将在整个 document 中搜索具有 id="number"

的元素

看起来你有多个这样的行,如果是的话

<tr>
    <td class='type'> Type name </td>
    <td class='number'> 102030 </td>
    <td class='software'> 1.0-Alpha </td>
</tr>

$('tbody tr').click(function() {
    var tdNumber = $(this).find('.number').html();
})

否则(即你只有一个元素具有给定的 id)那么你可以使用 id-selector 因为元素的 ID 必须是唯一的

var tdNumber = $('#number').html();

您在代码中遗漏了右大括号

    $('tbody tr').click(function() {
        var tdNumber = $(this).find('td:nth-child(2)').html();
    });
//---^