简体   繁体   English

如何在表行中隐藏值并在单击加号时显示它

[英]How to hide values in a table Row and show it when you click on plus

I am trying to hide my table value for one of my column since it has more than 10 values and to only show it when you click on plus. 我试图隐藏我的其中一列的表值,因为它具有10个以上的值,并且仅在单击加号时显示。 and otherwise when you click on minus it hides back the column values 否则,当您单击减号时,它将隐藏列值

My attempt to this is below . 我对此的尝试如下。 It works but not the way I want. 它有效,但不是我想要的方式。 It just shows the first value and not the rests when you click on plus. 单击加号时,它仅显示第一个值,而不显示其余值。 I haven't much knowledge in JS can someone please give me more clues on how I can handle this? 我对JS的了解不多,有人可以给我更多有关如何处理此问题的线索吗?

Here is my code 这是我的代码

HTML HTML

<table>
  <thead>
         <tr>
            <th>Forms</th>
         </tr>
     </thead>
     <tbody>

        <tr>
         <td id="details-control" class="details-open">

   <span id='content' class="d-none"><?= $row->getElement()?><br> </span>
  </td>

            </tr>
        </tbody>

CSS CSS

td.details-open {
    background: url("../img/details_open.png") no-repeat center center;
    cursor: pointer;
}
td.details-close {
    background: url("../img/details_close.png") no-repeat center center;
}

My script 我的剧本

    $(document).ready(function(){
    $('#details-control').on('click', function (e) {
        $("#content").removeClass("d-none")
        $("#details-control").removeClass("details-open")
        $("#details-control").addClass("details-close")

    });
});

The issue is for two reasons. 该问题有两个原因。 Firstly you're repeating the same id in multiple places when they must be unique within the DOM. 首先,当它们在DOM中必须唯一时,您要在多个位置重复相同的id Change these to classes if you want to group elements by common purpose or behaviour. 如果要按共同目的或行为对元素进行分组,请将其更改为类。

Secondly, you're attempting to select all the #content and #details-control elements when a cell is clicked. 其次,您试图在单击一个单元格时选择所有的#content#details-control元素。 You need to instead use DOM traversal to find content related to the cell which was clicked. 您需要使用DOM遍历来查找与被单击的单元格相关的内容。 To do that use the this keyword. 为此,请使用this关键字。

With that said, try the below example. 话虽如此,请尝试以下示例。 Note that the colours were only added to make the effect more obvious, as the image URLs won't work on this site. 请注意,仅添加颜色是为了使效果更明显,因为图像URL在此站点上将不起作用。

 $(document).ready(function() { $('.details-control').on('click', function(e) { var $cell = $(this).toggleClass('details-open details-closed'); $cell.find('.content').toggleClass('d-none'); }); }); 
 td.details-open { background: url("../img/details_open.png") no-repeat center center; cursor: pointer; color: #C00; } td.details-close { background: url("../img/details_close.png") no-repeat center center; color: #CCC; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <thead> <tr> <th>ID</th> <th>Forms</th> </tr> </thead> <tbody> <tr> <td> Key #1 </td> <td class="details-control details-open"> <span class="content d-none">Name #1<br> </span> <span class="content d-none">Name #2<br> </span> </td> </tr> <tr> <td> Key #2 </td> <td class="details-control details-open"> <span class="content d-none">Name #3<br> </span> <span class="content d-none">Name #4<br> </span> <span class="content d-none">Name #5<br> </span> </td> </tr> </tbody> </table> 

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

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