开发者问题收集

Bootstrap Accordion 抛出 TypeError:非法调用

2021-04-13
11800

我有一个像这样编写的手风琴

<!DOCTYPE html>
<html>
    <head>
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
    </head>
    <body>
    <div id="editTournamentAccordionWrapper" class="accordion accordion-flush">
        <div class="accordion-item">
            <h2 class="accordion-header" id="editTournamentAccordionHeading">
                <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#editTournamentAccordion" aria-expanded="false" aria-controls="editTournamentAccordion">
                    Card title
                </button>
            </h2>
            <div id="editTournamentAccordion" class="accordion-collapse collapse" aria-labelledby="editTournamentAccordionHeading" data-bs-parent="editTournamentAccordionWrapper">
                <div class="accordion-body">
                    <iframe class="w-100" style="height: 1024px;" src="www.example.com"></iframe>
                </div>
            </div>
        </div>
    </div>
    </body>
</html>

当我第一次单击手风琴标题时,手风琴不会打开,并且会抛出以下异常

Uncaught TypeError: 'querySelectorAll' called on an object that does not implement interface Element. selector-engine.js:18:59

知道原因吗?据我所知,这是我必须编写的最少代码才能使错误出现

1个回答

TLDR: data-bs-parent 属性中缺少 #

根据 Bootstrap 文档 data-bs-parent 接受

selector | jQuery object | DOM element

当您为属性传入 data-bs-parent="id" 时,它是纯文本,因此不属于任何类别。因此,您会收到 Uncaught TypeError:非法调用

工作示例:

<!DOCTYPE html>
<html>
    <head>
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">

    </head>
    <body>
    <div id="editTournamentAccordionWrapper" class="accordion accordion-flush">
        <div class="accordion-item">
            <h2 class="accordion-header" id="editTournamentAccordionHeading">
                <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#editTournamentAccordion" aria-expanded="false" aria-controls="editTournamentAccordion">
                    Card title
                </button>
            </h2>
            <div id="editTournamentAccordion" class="accordion-collapse collapse" aria-labelledby="editTournamentAccordionHeading" data-bs-parent="#editTournamentAccordionWrapper">
                <div class="accordion-body">
                  Card Body
<!--                    <iframe class="w-100" style="height: 1024px;" src="www.example.com"></iframe>-->
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
    </body>
</html>
readyplayer77
2021-04-13