开发者问题收集

Bootstrap 5 下拉菜单和导航:无法读取 null 的属性“children”

2021-08-03
2726

当我尝试在导航栏组件中使用带有导航选项卡的下拉按钮时,我收到此开发控制台错误消息“无法读取属性‘children of null”。

当我单击下拉项以在导航栏中显示导航链接时,我收到此开发控制台错误消息“无法读取属性‘children of null”

实例: https://codepen.io/themes4all/pen/bGWjMzZ

HTML:

<nav class="navbar navbar-expand-lg navbar-light bg-light py-3">
    <div class="container-fluid">
        <div class="row align-items-center">
            <div class="col">
                <div id="nav-tab" class="dropdown" role="tablist">
                    <button type="button" id="dropdownEx" class="btn btn-dark dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Select Item</button>
                    <ul class="dropdown-menu" aria-labelledby="dropdownEx">
                        <li>
                            <button type="button" role="tab" id="nav-example-1-tab" class="dropdown-item active" data-bs-toggle="tab" data-bs-target="#nav-example-1" aria-controls="nav-example-1" aria-selected="true">
                                Example 01
                            </button>
                        </li>
                        <li>
                            <button type="button" role="tab" id="nav-example-2-tab" class="dropdown-item" data-bs-toggle="tab" data-bs-target="#nav-example-2" aria-controls="nav-example-2" aria-selected="false">
                                Example 02
                            </button>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="col">
                <div class="tab-content">
                    <div role="tabpanel" id="nav-example-1" class="tab-pane fade show active" aria-labelledby="nav-example-1-tab">
                        <ul class="navbar-nav">
                            <li class="nav-item dropdown">
                                <a href="#" role="button" id="navbarDropdown" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Example 01</a>
                                <ul class="dropdown-menu" aria-labelledby="navbarDropdown"></ul>
                            </li>
                        </ul>
                    </div>
                    <div role="tabpanel" id="nav-example-2" class="tab-pane fade" aria-labelledby="nav-example-2-tab">
                        <ul class="navbar-nav">
                            <li class="nav-item dropdown">
                                <a href="#" role="button" id="navbarDropdown2" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Example 02</a>
                                <ul class="dropdown-menu" aria-labelledby="navbarDropdown2"></ul>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</nav>
3个回答

标签 (data-bs-toggle="tab") 应包含在 nav nav-tabs 中...

<nav class="navbar navbar-expand-lg navbar-light bg-light py-3">
    <div class="container-fluid">
        <div class="row align-items-center">
            <div class="col">
                <div id="nav-tab" class="dropdown" role="tablist">
                    <button type="button" id="dropdownEx" class="btn btn-dark dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Select Item</button>
                    <ul class="dropdown-menu nav nav-tabs" aria-labelledby="dropdownEx">
                        <li class="dropdown-item p-0">
                            <button type="button" role="tab" id="nav-example-1-tab" class="dropdown-item active" data-bs-toggle="tab" data-bs-target="#nav-example-1" aria-controls="nav-example-1" aria-selected="true"> Example 01 </button>
                        </li>
                        <li class="dropdown-item p-0">
                            <button type="button" role="tab" id="nav-example-2-tab" class="dropdown-item" data-bs-toggle="tab" data-bs-target="#nav-example-2" aria-controls="nav-example-2" aria-selected="false"> Example 02 </button>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="col">
                <div class="tab-content">
                    <div role="tabpanel" id="nav-example-1" class="tab-pane fade show active" aria-labelledby="nav-example-1-tab">
                        <ul class="navbar-nav">
                            <li class="nav-item dropdown">
                                <a href="#" role="button" id="navbarDropdown" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Example 01</a>
                                <ul class="dropdown-menu" aria-labelledby="navbarDropdown"></ul>
                            </li>
                        </ul>
                    </div>
                    <div role="tabpanel" id="nav-example-2" class="tab-pane fade" aria-labelledby="nav-example-2-tab">
                        <ul class="navbar-nav">
                            <li class="nav-item dropdown">
                                <a href="#" role="button" id="navbarDropdown2" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Example 02</a>
                                <ul class="dropdown-menu" aria-labelledby="navbarDropdown2"></ul>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</nav>

演示

警告: 这不是 Bootstrap 标签和下拉菜单的预期用途。下拉菜单并非设计用于切换标签,标签也并非用于导航栏。

Carol Skelly
2021-08-03

这并没有说明为什么会发生这种情况,但如果您想要解决方案。

删除与下拉列表项按钮关联的一些属性(例如,见下文)。根据您想要执行的操作,您可以为这些按钮设置 JavaScript 事件侦听器。或者您可以将按钮更改为锚标记并导航到其他页面。

<div id="nav-tab" class="dropdown" role="tablist">
    <button type="button" id="dropdownEx" class="btn btn-dark dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Select Item</button>
    <ul class="dropdown-menu" aria-labelledby="dropdownEx">
        <li>
            <button type="button" role="tab" id="nav-example-1-tab" class="dropdown-item active">
                Example 01
            </button>
        </li>
        <li>
            <button type="button" role="tab" id="nav-example-2-tab" class="dropdown-item">
                Example 02
            </button>
        </li>
    </ul>
</div>
Jason
2021-08-03

“叶”菜单项(例如 示例 1 示例 2 )不应包含 data-bs-toggle="dropdown"

Ermal
2023-02-18