简体   繁体   English

引导选项卡不更改内容

[英]Bootstrap tabs not changing content

I'm trying to integrate bootstrap tabs in one of our pages.我正在尝试将引导选项卡集成到我们的一个页面中。 I've copied the example from https://getbootstrap.com/docs/4.0/components/navs/#javascript-behavior but the tabs are not working correctly (they show up correctly but when I click on a tab, nothing happens).我从https://getbootstrap.com/docs/4.0/components/navs/#javascript-behavior复制了示例,但选项卡无法正常工作(它们显示正确,但当我单击选项卡时,没有任何反应) .

<ul class="nav nav-tabs" id="myTab" role="tablist">
    <li class="nav-item" role="presentation">
        <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
    </li>
    <li class="nav-item" role="presentation">
        <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button>
    </li>
    <li class="nav-item" role="presentation">
        <button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button>
    </li>
</ul>
<div class="tab-content" id="myTabContent">
    <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">home</div>
    <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">profile</div>
    <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">contact</div>
</div>

Both bootstrap and jQuery are installed: bootstrap 和 jQuery 都已安装:

"bootstrap": "^4.6.1",
"jquery": "^3.6.0",

and bundled correctly (has other pages use them as well).并正确捆绑(其他页面也使用它们)。

What could be the reason that the content isn't changing?内容没有改变的原因可能是什么? Do I have to add JS code manually?我必须手动添加JS代码吗?

Thanks in advance提前致谢

Edit: I've tried registering the click events for each tab.编辑:我已经尝试为每个选项卡注册点击事件。 When clicking on a tab, it console logs the corresponding message but does not show the correct content...单击选项卡时,控制台会记录相应的消息,但不显示正确的内容...

$(document).on('click', '#home-tab', function (event) {
    event.preventDefault();
    console.log('home clicked')
    $('#myTab a[href="#home"]').tab('show')
})

$(document).on('click', '#profile-tab', function (event) {
    event.preventDefault();
    console.log('profile clicked')
    $('#myTab a[href="#profile"]').tab('show')
})

$(document).on('click', '#contact-tab', function (event) {
    event.preventDefault();
    console.log('contact clicked')
    $('#myTab a[href="#contact"]').tab('show')
}) 

Following the official Bootstrap guide, it looks like you need to add the Javascript code manually:按照官方 Bootstrap 指南,您似乎需要手动添加 Javascript 代码:

Enable tabbable tabs via JavaScript (each tab needs to be activated individually):通过 JavaScript 启用可选项卡(每个选项卡需要单独激活):

$('#myTab a').on('click', function (e) {
  e.preventDefault()
  $(this).tab('show')
})

You can activate individual tabs in several ways:您可以通过多种方式激活单个选项卡:

$('#myTab a[href="#profile"]').tab('show') // Select tab by name
$('#myTab li:first-child a').tab('show') // Select first tab
$('#myTab li:last-child a').tab('show') // Select last tab
$('#myTab li:nth-child(3) a').tab('show') // Select third tab

如果您使用的是 Bootstrap 4,请将data-bs-toggle data-bs-target target 分别替换为data-toggle data-target

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM