简体   繁体   English

切换<TR>

[英]Toggle the <TR>

I am trying to do hide the belonging rows. 我试图隐藏所属的行。 For example if you click on 'Sub Title 1' which will then hide Item 1, Item 2 and Item 3 rows only. 例如,如果您单击“子标题1”,它将隐藏项目1,项目2和项目3行。

Example: 例:

<table border="1">
 <tbody>
  <tr class="head">
   <td> title </td>
  </tr>
  <tr class="sub-title">
     <td>Sub Title 1</td>
  </tr>
   <tr> <td>Item 1</td> </tr>
   <tr> <td>Item 2</td> </tr>
   <tr> <td>Item 3</td> </tr>
   <tr class="sub-title">
     <td>Sub Title 2</td>
   </tr>
   <tr> <td>Item 4</td> </tr>
   <tr> <td>Item 5</td> </tr>
   <tr> <td>Item 6</td> </tr>
  </tbody>
</table>

- -

$('.sub-title').click( function() {
    $(this).parent().nextUntil('.sub-title').toggle();
})

It doesn't seem to work... 它似乎不起作用......

You have to toggle manually: 您必须手动切换:

$(".sub-title").on("click",function() {
    $(this).nextUntil(".sub-title").each(function() {
        this.style.display = this.style.display == "none" ? "" : "none";
    });
});

nextUntil selects for siblings, not children. nextUntil选择兄弟姐妹,而不是孩子。 Remove the "parent" from your call. 从通话中删除“父母”。

Edited to respond to further question about excluding some rows based on class. 编辑回答关于基于类排除某些行的进一步问题。 Obviously you can also accommodate Kolink's response about preventing toggle's "display: block" overwriting the default "display: table-row", but as long as you test in all supported browsers, I don't see any problem with using toggle. 显然,您也可以接受Kolink关于防止切换的“display:block”覆盖默认“display:table-row”的响应,但只要您在所有支持的浏览器中进行测试,我就不会发现使用切换有任何问题。

$('.sub-title').click( function() {
   $(this).nextUntil('.sub-title').each(function() {
        if (!$(this).hasClass('order')) 
            $(this).toggle();
    });
});

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

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