2017-03-01 177 views
-1
<table> 
    <tr> 
     <td><span data-....></span></td> // 1 
     <td><a href="">....</a><td> 
     <td><span data-....></span></td> // 2 
     <td><a href="">....</a><td> 
     <td><span data-....></span></td> // 3 
     <td><a href="">....</a><td> 
    </tr> 
</table> 

當我點擊跨度時,如何找到下一個跨度?獲取td中的下一個元素

跨度轉換的輸入,當我按TAB鍵,我想進入下一個跨度...

每個跨度有AA HREF ..如果沒有之間的HREFs它的工作

$('table td span').hover(function() { 
    $(this).css('cursor','pointer'); 
}); 

$(document).on('keydown', 'table td input', function(event) { 
    var target = $(this); 

    if (event.which == 9) { 
     event.preventDefault(); 

     console.log('Tab pressed'); 

     var span = target.parent().next().find('span'); 

     span.click(); 

     console.log(span.data('date')); 
    } 
}); 
+3

$(本).parent()的next() –

+0

我想'''VAR跨度= target.parent()未來( 'TD')找到( '跨');'''不起作用 – yooouuri

回答

5
$('table td').on('click', 'span', function(event) { 
    $(this).parent()   // the parent of the span 
      .next()   // the next element to that parent (href td) 
      .next()   // the next element to that next element (span td) 
      .find('span'); // the span children of the first 
}); 
+1

'.first()'調用是不必要的 – Andreas

+0

@Andreas是啊!只是測試它,你是對的! –

+0

@Yooouuri請解釋更多!示例中的代碼片段工作得很好! –