代码之家  ›  专栏  ›  技术社区  ›  Leo

jquery gt()获取当前索引

  •  2
  • Leo  · 技术社区  · 15 年前

    我在用 gt() 向表中添加列。

    我需要使用行号和列号来构建单元ID。

    获取列号很简单:

    var c = $("#gridLayout tr:first td").length;
    

    但是如何从gt()中获取行号的当前索引呢?

    $("#gridLayout tr:gt(0)").append('<td>.......</td>');
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   user113716    15 年前

    一种方法是使用内置索引参数 .each() .

    这样可以得到一个索引号 没有 必须调用另一个方法。

    试一试: http://jsfiddle.net/8V4AY/

       // Reference index --------------v
    $("#gridLayout tr").each(function( idx ) {
           // idx contains the current index number,
           //    without having to call another method
    
         $(this).append('<td>.......</td>');
    });
    

    如果要跳过第一行:

    $("#gridLayout tr:gt(0)")...
    

    你可以调整 idx 如果你需要的话1点

    idx++;
    

    http://jsfiddle.net/8V4AY/1/

        2
  •  2
  •   HurnsMobile    15 年前

    我不确定您在这里的目标是什么,但是类似于以下内容的东西会返回行索引并附加任何内容:

    $('#gridlayout tr').each( function() {
        $(this).append('<td> Things.... </td>');
        alert($(this).index());
    })
    

    如果你能发布更多的细节,我很乐意提供更多的信息。