Jquery在表中选择第二个单元格

时间:2011-07-11 15:19:27

标签: jquery

这是脚本上的代码,我需要从第二个单元格中获取内容。 该表没有id,但是在id为'Chat'的div中

我可以选择第4行:

var row = $("#chat tr:nth-child(4)");

但不是第二个细胞:

var cellContent = $("#chat td:nth-child(4)").html();

我无法发布图片或表格标签,因此这是表格图像及其代码的链接。 http://img577.imageshack.us/img577/8871/chattable.png

1 个答案:

答案 0 :(得分:4)

这是一个小提琴,希望有助于你的jquery问题。 http://jsfiddle.net/CrumK/1/

$(document).ready(function() {
    // this reads: find the element with ID chat and inside that find every TR element that is the 2nd child of its parent (meaning TABLE).
    $("#chat tr:nth-child(2)").css("color", "blue");  // select the second row

    // this reads: find the element with ID chat and inside that find every TD element that is the 2nd child of its parent (meaning TR)    
    $("#chat td:nth-child(2)").css("background", "red"); // select every second TD

    // select the second TD of the second TR inside the #chat element
    $("#chat tr:nth-child(2) td:nth-child(2)").css("background", "green");  // select the second row
});