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

jquery:选择“this”子元素

  •  1
  • Breezer  · 技术社区  · 15 年前

    我真的对这个特殊的障碍非常着迷,我手头的问题是我有一个非常简单的表,上面有行和所有行。我要做的是,当您单击一行时,假设这个脚本读取存储在最后一行中的链接 td 在同一排,然后指引你到那里。

    到目前为止我想到的是

     $('tr td').click(
         function (){
            alert($(this+':parent td:last-child a:first').attr('href'));
         }
     );
    

    我尝试了100种不同的方法,要么得到一个错误/未定义的结果,要么只得到中间行不能按预期工作的最后一行/第一行的结果。

    非常感谢您的帮助

    表如下所示

    http://www.jsfiddle.net/xK7Mg/1/

    5 回复  |  直到 9 年前
        1
  •  3
  •   Jacob Relkin    15 年前

    我想你是想这样做的:

    $('tr td').click(function() {
       window.location.href = $(this).parent().find('td:last-child a:first').attr('href');
    });
    
        2
  •  1
  •   Yi Jiang G-Man    15 年前

    我认为你需要的是:

    $('tr').click(function(){
        window.location = $(this).find('td:last a:first').attr('href');
    });
    

    此脚本将导致每个表行在单击时重定向到最后一个表单元格中第一个定位元素中引用的位置。

        3
  •  1
  •   Nick Craver    15 年前

    使用 .siblings() 不过,在这种情况下 .parent().children() 同样有效,如:

    $('tr td').click(function() {
      window.location.href = $(this).siblings('td:last').find('a').attr('href');
    });
    

    不过,再往前走一步,不要 click 每个单元格的处理程序,使用 .delegate() 要为整个表附加一个,如下所示:

    $('#tableID').delegate('td', 'click', function() {
      window.location.href = $(this).siblings('td:last').find('a').attr('href');
    });
    

    You can try it out here .

        4
  •  0
  •   jargalan    15 年前
    $('tr').click(
         function (){
            window.location = $(this).find("td:last a:first").attr('href');
         }
     );
    
        5
  •  0
  •   Gregg    15 年前

    http://jsfiddle.net/ppw8z/

     $('tr').click(function() {
        window.location = $(this).find('td>a').attr('href');    
     });
    
    推荐文章