Bootstrap v5.2.1 Modal初始化报错:TypeError无法读取backdrop属性
Bootstrap v5.2.1 Modal 报错:TypeError Cannot read properties of undefined (reading 'backdrop')
在使用Bootstrap v5.2.1的Modal组件时,出现报错:TypeError Cannot read properties of undefined (reading 'backdrop')。该问题已在bug tracker中记录,但无法稳定复现;测试多种浏览器、浏览器版本及操作系统后,仅部分浏览器会触发此问题。
初始化Modal的JavaScript代码
if (bootstrap == undefined) { document.getElementById("browser-not-supported-container").classList.remove("d-none"); } else { modal1 = new bootstrap.Modal(document.getElementById('modal1')); modal2 = new bootstrap.Modal(document.getElementById('modal2')); modal3 = new bootstrap.Modal(document.getElementById('modal3')); }
Modal的HTML结构
<!-- Modal 1--> <div class="modal fade text-start" data-bs-backdrop="static" data-bs-keyboard="false" id="modal1" tabindex="-1" role="dialog"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-header"> <!-- Header --> </div> <div class="modal-body"> <!-- Body --> </div> </div> </div> </div> <!-- Modal 2--> <div class="modal fade text-start" id="modal2" tabindex="-1" role="dialog"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-body text-center"> <!-- Body --> </div> </div> </div> </div> <!-- Modal 3--> <div class="modal fade text-start" id="modal3" tabindex="-1" role="dialog"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-body text-center"> <!-- Body --> </div> </div> </div> </div>
具体报错信息
TypeError Cannot read properties of undefined (reading 'backdrop') https://example.com/:lineNo3:colNo3 Ni._initializeBackDrop https://example.com/:lineNo2:colNo2 new Ni https://example.com/:lineNo1:colNo1 HTMLDocument.<anonymous>
注:lineNo1对应modal2的初始化位置。
已尝试的操作(无效)
手动为每个Modal实例添加配置项后,报错仍未消除:
modal1 = new bootstrap.Modal(document.getElementById('modal1'), { backdrop: "static", keyboard: false }); modal2 = new bootstrap.Modal(document.getElementById('modal2'), { backdrop: "static", keyboard: false }); modal3 = new bootstrap.Modal(document.getElementById('modal3'), { backdrop: "static", keyboard: false });
内容的提问来源于stack exchange,提问作者Naga Sai Rohith Devarasetty
相关产品推荐
相关产品推荐

