开发者问题收集

Bootstrap 5:通过 JavaScript 显示下拉菜单时出错

2021-04-18
4408

我试图通过 JS 显示下拉菜单。不幸的是,Bootstrap 5 中似乎有一个错误。 在此示例代码中,显示模式有效,但显示下拉菜单会引发异常: Uncaught TypeError:无法读取 _e.show (dropdown.js:140) 中未定义的属性“classList”

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>

<body>

<div id="testDropdown" class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
        Dropdown button
    </button>
    <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
    </ul>
</div>

<div id="testModal" class="modal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Modal title</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>Modal body text goes here.</p>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

<script>

    let testModal = document.getElementById("testModal");
    //new bootstrap.Modal(testModal).show(); //works

    let testDropdown = document.getElementById("testDropdown");
    new bootstrap.Dropdown(testDropdown).show(); //doesnt work

</script>

</body>
</html>

我做错了什么还是这是一个错误?

提前致谢。

2个回答

元素应为 dropdown-trigger ,而不是父元素 dropdown ...

<div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" id="testDropdown" type="button" data-bs-toggle="dropdown"> Dropdown button </button>
    <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
    </ul>
</div>

演示

注意 :Bootstrap 5 包含一个新的 auto-close 选项。 必须 将其设置为 false,以便以编程方式从下拉菜单的父级外部触发下拉菜单。

Carol Skelly
2021-04-18

我不用按钮就让它工作了:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Dropdown Test</title>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
</head>

<body>

<div class="container-fluid">

    <div class="row justify-content-center">
        <div class="w-75 mt-2">

            <div class="input-group">
                <span class="input-group-text"><i class="bi bi-search"></i></span>
                <input type="text" class="form-control" placeholder="Search for something">
                <button class="btn btn-primary" type="button">Search</button>
            </div>

            <div class="dropdown">
                <div data-bs-toggle="dropdown" id="DROPDOWN_SEARCH"></div>
                <div class="dropdown-menu w-100">
                    <a class="dropdown-item" href="#">Result 1</a>
                    <a class="dropdown-item" href="#">Result 2</a>
                    <a class="dropdown-item" href="#">Result 3</a>
                </div>
            </div>

        </div>
    </div>

</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

<script>
    let searchDropdown = new bootstrap.Dropdown("#DROPDOWN_SEARCH");
    searchDropdown.show();
</script>

</body>

</html>
nhcodes
2021-04-18