jquery children()选择器没有选择合适的元素

时间:2010-11-01 18:43:35

标签: jquery jquery-selectors

我正在使用jquery尝试在简单表中选择一个项目。当我打电话给

$('.broadcast_item_even').mouseover(function(event) {

    //SET TR GLOW EFFECT
    $(this).attr('class', 'broadcast_item_hover');

    //ALERT THE VALUE 
    alert( $(this).children(2).html());

});

在这个表对象上

<table style="color: rgb(0, 0, 255);" id="Table1">  
    <tbody>
        <tr class="broadcast_item_even">
            <td>
                <img height="50px" width="50px" alt="user avatar" src="../../Avatar/default-user.jpg">

            </td>
            <td>                 
                jimbo60
            </td>

            <td>
                10.8 miles
            </td>

        </tr>
    <tbody>
</table>

结果打印

<img height="50px" width="50px" alt="user avatar" src="../../Avatar/default-user.jpg">

而不是

10.8 miles

这是我所期待的。有没有人知道为什么会发生这种情况?如果是这样,我们将非常感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

如果你想要第三个孩子,你需要:eq(2).eq(2),如下所示:

$(this).children().eq(2).html()
//or:
$(this).children(":eq(2)").html()

You can test it out here.children()函数采用选择器,而不是索引。

答案 1 :(得分:0)

尝试$(this).children('td:last').html()

答案 2 :(得分:0)

您还可以使用以下代码获取第三个td的文本。

  

警报($(本).find( “TD”)(2)式的.html());

相关问题