英文:
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 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<div class="row">
<div class="col-4">
<div class="list-group" id="list-tab" role="tablist">
<a class="list-group-item list-group-item-action active" id="list-home-list" data-toggle="tab" href="#list-home" role="tab" aria-controls="home">Home</a>
<a class="list-group-item list-group-item-action" id="list-profile-list" data-toggle="tab" href="#list-profile" role="tab" aria-controls="profile">Profile</a>
</div>
</div>
<div class="col-8">
<div class="tab-content" id="nav-tabContent">
<div class="list-group" id="list-tab" role="tablist">
<a id="list-home-page" data-toggle="tab" href="#aa" role="tab" aria-controls="a">
Link 1
</a>
<a id="list-profile-page" data-toggle="tab" href="#bb" role="tab" aria-controls="b">
Link 2
</a>
</div>
</div>
</div>
</div>
<br><br>
<div class="tab-content" id="nav-tabPage">
<div class="tab-pane fade show active" id="aa" role="tabpanel" aria-labelledby="aa">
Page 1
</div>
<div class="tab-pane" id="bb" role="tabpanel" aria-labelledby="bb">
Page 2
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
<!-- 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)。
有一件事我要提醒你,不要简单地从文档中复制代码并仅仅插入你想要的值。它可能会起作用,但不会教会你任何东西。
英文:
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
<ul class="list-group">
<li class="list-group-item">An item</li>
<li class="list-group-item">A second item</li>
</ul>
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:
<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>
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论