简体   繁体   English

jQuery在表td中显示和隐藏元素

[英]jQuery show & hide elements in table td

I'm struggling a little bit with the following. 我在以下方面苦苦挣扎。 I have a table with 2 rows and within the rows three TD's. 我有一个有2行的表,在行中有3个TD。 Each td contains and <img> and a <div class="overlay"> . 每个td包含一个<img>和一个<div class="overlay">

Some of the .overlay divs are open by default. 默认情况下,某些.overlay div是打开的。 I want these to close too if there is a click on a TD . 如果要单击TD,我也希望它们也关闭

Goal: If you click on an TD a want to open/show that particular overlay and close all others (also the overlay's that are open by default). 目标:如果单击TD,则要打开/显示该特定的叠加层并关闭所有其他叠加层(默认情况下也处于打开状态的叠加层)。 The <img/> can stay where it is. <img/>可以留在原处。

HTML Structure HTML结构

<table id="table_overview">
  <tr>
     <td><img src="foo.jpg"/><div class="overlay">text</div></td>
     <td><img src="foo.jpg"/><div class="overlay hidden">text</div></td>
     <td><img src="foo.jpg"/><div class="overlay hidden">text</div></td>
  </tr>
  <tr>
     <td><img src="foo.jpg"/><div class="overlay hidden">text</div></td>
     <td><img src="foo.jpg"/><div class="overlay hidden">text</div></td>
     <td><img src="foo.jpg"/><div class="overlay">text</div></td>
  </tr>
</table>

jQuery (not working well) jQuery (效果不佳)

$(#table_overview td).each(function(){
   $(this).on('click', function(){
       if($(this).find('.overlay').hasClass('hidden')){
          $(this).find('.overlay').show();
       }
   }, function(){
       // second click on td close this overlay.
   });
});

Assuming that it is the hidden class that hides the overlay, so just toggle the hidden class 假设是隐藏类覆盖了覆盖层,所以只需切换隐藏类即可

$("#table_overview tr td").each(function(){
   $(this).on('click', function(){
       $(this).find('.overlay').toggleClass('hidden');
   });
});

or simply 或简单地

$("#table_overview tr td").on('click', function(){
       $(this).find('.overlay').toggleClass('hidden');
});

If you want to close all other overlay div's that are not hidden for a particular row, then 如果要关闭对特定行未隐藏的所有其他覆盖div,则

$("#table_overview tr td").on('click', function(){
       $(this).parent().find('.overlay').addClass( "hidden" );
       $(this).find('.overlay').removeClass('hidden');
});

If you want to close all other overlay div's that are not hidden for complete table, then 如果您想关闭所有未覆盖完整表格的重叠式div,则

$("#table_overview tr td").on('click', function(){ $(this).parent().parent().find('.overlay').addClass( "hidden" ); $(this).find('.overlay').removeClass('hidden'); }); $(“#table_overview tr td”)。on('click',function(){$(this).parent()。parent()。find('。overlay')。addClass(“ hidden”); $( this).find('。overlay')。removeClass('hidden');});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM