【问题标题】:change the icon in expandable view更改可展开视图中的图标
【发布时间】:2015-12-02 20:12:43
【问题描述】:

我有一张可展开视图的表格。单击加号它会扩展,但我希望在视图处于展开形式时将加号更改为减号(fa fa-plus to fa fa-minus),并在视图处于压缩形式时返回加号,谁能告诉我该怎么做所以(代码@fiddle)

 <script>
    $(document).ready(function(){
        $("#report tbody tr:odd td:first-child").click(function(){
            $this=$(this);
            $this.parent().next("tr").toggle();
            $this.find(".arrow").toggleClass("up");
        });
    });
        </script>

       <table id="report" border="1" style="width:100%" >
        <tr>
            <th> First </th>
            <th> Second </th>
            <th> Third </th>
        </tr>

        <tr>
             <td><i class="fa fa-plus"></i></td>
             <td>1</td>
             <td>1</td>
        </tr>   
        <tr>
            <td colspan="10">
                dummy text 1<br>
                dummy text 1<br>
                dummy text 1<br>
            </td>
        </tr>   
    </table>

【问题讨论】:

标签: javascript jquery css


【解决方案1】:

如果您想要一种简单的方法,则将具有属性 display:none 和 display:block 的两个图标与公共类一起使用,然后切换它们。

见action

【讨论】:

    【解决方案2】:

    将某些类应用于父级以识别您何时扩展了元素。对于展开的元素,隐藏加号图标并显示减号图标,反之亦然。

    $(document).ready(function() {
      $('.toggler').click(function() {
        $(this).toggleClass('active');
      });
    });
    .toggler {
      padding: 3px 5px;
      border: 1px solid #ddd;
      float: left;
    }
    .icon-minus {
      display: none;
    }
    .active .icon-plus {
      display: none;
    }
    .active .icon-minus {
      display: inline;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="toggler">
      <span class="icon icon-plus">+</span>
      <span class="icon icon-minus">-</span>
    </div>

    【讨论】:

      【解决方案3】:

      根据我的观察,我没有找到任何具有 arrow 类的 element,但我认为您需要在 collapse 和 expand 上的 fa-plus 和 fa-minus 之间切换。所以你可以实现如下:

      $(document).ready(function(){
           $("#report tbody tr:odd td:first-child").click(function(){
               $this=$(this);
               $this.parent().next("tr").toggle();
               $this.find("i").toggleClass("fa-plus fa-minus");
               //you find the "i" element in td and toggle its fa-plus and fa-minus
          });
      });
      

      DEMO

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-14
        • 2015-05-31
        相关资源
        最近更新 更多