2011-09-03 72 views
1

我有兩列...右列我想打開並顯示內容,當它的標題被點擊,或者它的相應值在左側列被點擊...並關閉時,另一個值被點擊。謝謝您的幫助。列點擊問題

<table> 
    <tr> 
     <th>1</th> 
     <td> 
      <div class="showMeOnClick">stuff</div> 
     </td> 
    </tr> 
    <tr> 
     <th>2</th> 
     <td> 
      <div class="showMeOnClick">different stuff</div> 
     </td> 
    </tr> 
    <tr> 
     <th>3</th> 
     <td> 
      <div id="showMeOnClick">stuff</div> 
     </td> 
    </tr> 
</table> 

$('th').each(function() { 
    $(this).click(function() { 
     $(this).find('td').toggle(); 
    }); 
}); 

回答

2
$('th').click(function(){ 
    $('.showMeOnClick').hide(); // hide everything 
    $(this).siblings('td').find('.showMeOnClick').show(); // show div in the same row 
}); 

,如果你想捕捉如果DIV是媒體鏈接可見(不隱藏它,並再次顯示,使用類)

$('th').click(function(){ 
    if ($(this).is('.active') == false) { 
     $('th').removeClass('active'); 
     $('.showMeOnClick').hide(); // hide everything 
     $(this).addClass('active').siblings('td').find('.showMeOnClick').show(); // show div in the same row 
    } 
});