【问题标题】:toggle-data=collapse expend but does not closetoggle-data=collapse 消耗但不关闭
【发布时间】:2021-05-30 22:18:04
【问题描述】:

我知道这个问题已经被问过好几次了,但我能找到的每一个答案都不起作用。 可展开的行会展开,但在重新点击时不会关闭。

这是我的代码:

 <tr class="accordion-toggle" id="myGroup">
  <td><button id="{{other_profile.user.id}}b" data-toggle="collapse" data-target="#{{other_profile.user.id}}"class="btn-accordion" aria-expanded="false" data-parent="#myGroup">+</button></td>
  <td data-label="Name">{{ other_profile.user.first_name }}</td>
  <td data-label="Last Name">{{ other_profile.user.last_name }}</td>
  <td data-label="Circle">{{ met_profile.circle.name }}</td>
  <td data-label="Last Met">{{ met_profile.last_met.date }}</td>
 </tr>
 <tr style="border-bottom: 1px solid #D3D3D3;">
  <td colspan="6" class="hiddenRow" style="padding: 0px;">
   <div class="accordian-body collapse" id="{{other_profile.user.id}}">
    <div class="expanded-row">

      (content of the expanded row)

    </div>
   </div>
  </td>
 </tr>

我的 JavaScript

{% block javascript %}
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <script>
    jQuery('button').click( function(e) {
     jQuery('.collapse').collapse('hide');
    });
  </script>
  <script>
    $(document).ready(function() {
      var queryString = window.location.hash;
      $(queryString).trigger('click');
    });
  </script>
{% endblock %}

(我的代码的 base.html 中也有链接 https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"(使用 Django)。

如果我删除引导脚本,它可以工作,但其他 2 个功能不起作用(打开另一个时关闭展开的行,当我从另一个页面单击按钮时打开一行)

欢迎任何帮助,谢谢。

【问题讨论】:

    标签: javascript html jquery css twitter-bootstrap


    【解决方案1】:

    .accordian-body {
      display: none;
    }
    <tr class="accordion-toggle" id="myGroup">
      <td><button data-target="#{{other_profile.user.id}}"class="btn-accordion"
                  onclick="openDiv('1')">+</button></td>
      <td data-label="Name">{{ other_profile.user.first_name }}</td>
      <td data-label="Last Name">{{ other_profile.user.last_name }}</td>
      <td data-label="Circle">{{ met_profile.circle.name }}</td>
      <td data-label="Last Met">{{ met_profile.last_met.date }}</td>
     </tr>
     <tr style="border-bottom: 1px solid #D3D3D3;">
      <td colspan="6" class="hiddenRow" style="padding: 0px;">
       <div class="accordian-body collapse" id="1">
        <div class="expanded-row">
    
          (content of the expanded row)
    
        </div>
       </div>
      </td>
     </tr>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
     <script>
            function openDiv(x) {
                $('#' + x).toggle();
            }
        </script>

    【讨论】:

    • 如此接近,它确实关闭了该行但在...之后重新打开它...
    • 通过添加此函数修复它 openDiv(x) { $('#'+x).toggle(); $('.accordian-body').not('#'+x).css("display", "none") }
    • @YohavRn 很酷。如果您希望它在开始时打开,请删除 CSS 属性。
    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2015-07-29
    • 2021-03-12
    • 2014-04-10
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多