【问题标题】:Bootstrap collapse dynamic 'data-target' not working as desired引导折叠动态“数据目标”无法按预期工作
【发布时间】:2019-08-28 19:44:55
【问题描述】:

我想通过引导程序 collapse 显示一些帖子。我使用 post id 在 bootstrap collapse 中设置了动态 data-targetid。它不工作。

     <div class="container">
      <div class="accordion">
        <div class="card">
          <% hadiths.forEach(function(hadith){ %>
          <div class="card-header" id="headingOne" data-toggle="collapse" 
data-target="#<%= hadith._id %>" aria-expanded="false" aria-controls="<%= hadith._id %>">
            <h2 class="mb-0">
              <h3>
                <div class="row">
                    <div class="col-11">
                <%= hadith.serial %>- <%= hadith.title %>
                  </div>
                </div>
              </h3>
            </h2>
          </div>
          <div id="<%= hadith._id %>" class="collapse id" aria-labelledby="headingOne">
            <div class="card-body">
                  <%- hadith.description %>
            </div>
          </div>
    <% }); %>
        </div>
      </div>
    </div>

但是如果我在&lt;%= hadith._id %&gt; 前面添加一些字符串,那么它就像a&lt;%= hadith._id %&gt; 一样工作。我需要设置它&lt;%= hadith._id %&gt;。请告诉我我做错了什么。

感谢您的关注。

【问题讨论】:

标签: javascript html css bootstrap-4 ejs


【解决方案1】:

您的&lt;%= hadith._id %&gt; 是否以特殊键或数字开头? 如果是这样,这可能是问题

【讨论】:

  • hadith._id 是类似5d656babc0888435447ae7c8 的字符串。不同的帖子会有所不同。
  • 我认为以数字和特殊键开头的 ID 和类存在一些问题,您可以尝试使用字符串吗?从一个字符开始?如果您需要 js 中的 id,您可以将其添加为数据属性 Somethink,例如 id="collaps-&lt;%= [some index made by the foreach...]%&gt;"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
相关资源
最近更新 更多