“Bootstrap嵌套列表组在选项卡内容中同时显示两个项目”

huangapple go评论60阅读模式
英文:

Bootstrap Nested List Group shows both items in the Tab Content

问题

I am trying to create a page navigation feature in HTML using the Bootstrap List-group class.

How the feature should work:

When I click Home, show link 1 only, and when I further click Link 1, show Page 1 only.

Likewise, when I click Profile, show link 2 only, and when I further click Link 2, show Page 2.

The links work fine and show the right items.

The problem is, both Link 1 and Link 2 show up at the same time when I click either the Home link or the Profile link.

英文:

I am trying to create a page navigation feature in HTML using the Bootstrap List-group class.

How the feature should work:

When I click Home, show link 1 only, and when I further click Link 1, show Page 1 only.

Likewise, when I click Profile, show link 2 only, and when I further click Link 2, show Page 2.

The links work fine and show the right items.

The problem is, both Link 1 and Link 2 show up at the same time when I click either the Home link or the Profile link.

Thanks!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;div class=&quot;row&quot;&gt;
  &lt;div class=&quot;col-4&quot;&gt;
    &lt;div class=&quot;list-group&quot; id=&quot;list-tab&quot; role=&quot;tablist&quot;&gt;
      &lt;a class=&quot;list-group-item list-group-item-action active&quot; id=&quot;list-home-list&quot; data-toggle=&quot;tab&quot; href=&quot;#list-home&quot; role=&quot;tab&quot; aria-controls=&quot;home&quot;&gt;Home&lt;/a&gt;
      &lt;a class=&quot;list-group-item list-group-item-action&quot; id=&quot;list-profile-list&quot; data-toggle=&quot;tab&quot; href=&quot;#list-profile&quot; role=&quot;tab&quot; aria-controls=&quot;profile&quot;&gt;Profile&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;col-8&quot;&gt;
    &lt;div class=&quot;tab-content&quot; id=&quot;nav-tabContent&quot;&gt;
      &lt;div class=&quot;list-group&quot; id=&quot;list-tab&quot; role=&quot;tablist&quot;&gt;
        &lt;a id=&quot;list-home-page&quot; data-toggle=&quot;tab&quot; href=&quot;#aa&quot; role=&quot;tab&quot; aria-controls=&quot;a&quot;&gt;
        Link 1
        &lt;/a&gt;
        &lt;a id=&quot;list-profile-page&quot; data-toggle=&quot;tab&quot; href=&quot;#bb&quot; role=&quot;tab&quot; aria-controls=&quot;b&quot;&gt;
        Link 2
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;br&gt;&lt;br&gt;

&lt;div class=&quot;tab-content&quot; id=&quot;nav-tabPage&quot;&gt;
  &lt;div class=&quot;tab-pane fade show active&quot; id=&quot;aa&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;aa&quot;&gt;
    Page 1
  &lt;/div&gt;
  &lt;div class=&quot;tab-pane&quot; id=&quot;bb&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;bb&quot;&gt;
    Page 2
  &lt;/div&gt;
&lt;/div&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 1

根据你的代码,看起来你可能在尝试创建这个导航时混淆了两个概念。

通常,list-group 和 list-group-item 用在无序列表元素内,就像这样:

<ul class="list-group">
  <li class="list-group-item">An item</li>
  <li class="list-group-item">A second item</li>
</ul>

虽然如果你愿意,你仍然可以使用相同的 HTML 元素(ul 和 li),但在使用它进行导航时,你需要更改 Bootstrap 中的类。

这不仅涉及样式,还涉及一些不同的 JavaScript 使用方式。

示例:

<ul class="nav">
  <li class="nav-item">
    <a class="nav-link active" href="#">Active</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled">Disabled</a>
  </li>
</ul>

我建议从这个部分开始查阅文档(下面的链接),然后重新设计你的代码以匹配它。

Bootstrap 4 Tab - JavaScript Behavior

现在,如果你想让用户保持在同一页,但在同一页上显示不同的内容层,通常我会建议使用选项卡(tabs)。如果你正在寻找页面之间的导航,比如:

  • example.com/home
  • ../about
  • ../contact

考虑使用导航栏(navbar)。

Bootstrap 4 - Navbar

有一件事我要提醒你,不要简单地从文档中复制代码并仅仅插入你想要的值。它可能会起作用,但不会教会你任何东西。

英文:

Looking at your code it seems you've maybe conflated two concepts together in trying to create this navigation.

Usually list-group and list-group-item are used within unordered list elements like this

&lt;ul class=&quot;list-group&quot;&gt;
  &lt;li class=&quot;list-group-item&quot;&gt;An item&lt;/li&gt;
  &lt;li class=&quot;list-group-item&quot;&gt;A second item&lt;/li&gt;
&lt;/ul&gt;

While you can still used the same HTML elements (ul and li) if you want, you have to change the classes in bootstrap when you want to use it for navigation.

This isn't just the styling affect but also some of the JS that's used is going to be different.

Example:

&lt;ul class=&quot;nav&quot;&gt;
  &lt;li class=&quot;nav-item&quot;&gt;
    &lt;a class=&quot;nav-link active&quot; href=&quot;#&quot;&gt;Active&lt;/a&gt;
  &lt;/li&gt;
  &lt;li class=&quot;nav-item&quot;&gt;
    &lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Link&lt;/a&gt;
  &lt;/li&gt;
  &lt;li class=&quot;nav-item&quot;&gt;
    &lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Link&lt;/a&gt;
  &lt;/li&gt;
  &lt;li class=&quot;nav-item&quot;&gt;
    &lt;a class=&quot;nav-link disabled&quot;&gt;Disabled&lt;/a&gt;
  &lt;/li&gt;
&lt;/ul&gt;

I recommend checking the documentation starting in this section (link below) and refactor your code to match.

Bootstrap 4 Tab - JavaScript Behavior

Now, tabs I would usually suggest if you want users to stay on one page but show different layers of content on that single page. If you are looking for navigation between pages like

  • example.com/home
  • ../about
  • ../contact

Consider using the navbar instead.

Bootstrap 4 - Navbar

One thing I would warn against is copying the code from the documentation and simply plugging in the values you want. It may work but it won't teach you anything.

huangapple
  • 本文由 发表于 2023年5月22日 17:47:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定