Bootstrap 5.2.3 addEventListener不工作

qzlgjiam  于 2023-09-28  发布在  Bootstrap
关注(0)|答案(1)|浏览(116)

我有示例页面和引导对话框是从服务器返回。我想把它放在DIV里面。
一旦点击,它会给我这个错误:
未捕获的类型错误:myModal.addEventListener不是函数
在这一行:

myModal.addEventListener('show.bs.modal', function () {

为什么?谢谢!

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" type="text/css" />
</head>

<body>
  <p><a href="#" onclick="showSubscriptionInfo();">Open dialog</p>
     <div id="myDiv"></div>
   </body>
   
   <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> 
   <script>
   function     showSubscriptionInfo(){
     var data = ''+
      '<div class="modal fade" id="subsModal" tabindex="-1" aria-labelledby="exampleModalLabel">'+
      '  <div class="modal-dialog">'+
      '    <div class="modal-content" style="height:450px">'+
      '      <div class="modal-body">'+
               '<p>xxx</p>'+
      '      </div>'+
          '<div class="modal-footer">'+
          '  <button type="button" class="button" data-bs-dismiss="modal">Close</button>'+
          '</div> '+
      '    </div>'+
      '  </div>'+
      '</div>';
    
     document.getElementById('myDiv').innerHTML = data;
     var myModal = new bootstrap.Modal(document.getElementById('subsModal'));
     myModal.show();
     
     myModal.addEventListener('show.bs.modal', function () {
       alert(1);
     }, { once: true });
     myModal.addEventListener('hide.bs.modal', function () {
       alert(2);
     }, { once: true });
     
   }
   </script>
hmmo2u0o

hmmo2u0o1#

bootstrap模态中使用的事件必须在用于示例化模态的div(document.getElementById('subsModal'))上跟踪,而不是在bootstrap.Modal对象(new bootstrap.Modal(document.getElementById('subsModal')))上跟踪。
查看modal docs和模态事件文档中的更多内容。

function showSubscriptionInfo() {
  var data = '' +
    '<div class="modal fade" id="subsModal" tabindex="-1" aria-labelledby="exampleModalLabel">' +
    '  <div class="modal-dialog">' +
    '    <div class="modal-content" style="height:450px">' +
    '      <div class="modal-body">' +
    '<p>xxx</p>' +
    '      </div>' +
    '<div class="modal-footer">' +
    '  <button type="button" class="button" data-bs-dismiss="modal">Close</button>' +
    '</div> ' +
    '    </div>' +
    '  </div>' +
    '</div>';

  document.getElementById('myDiv').innerHTML = data;
  var subsModal = document.getElementById('subsModal');
  var myModal = new bootstrap.Modal(subsModal);
  subsModal.addEventListener('show.bs.modal', function() {
    alert(1);
  }, {
    once: true
  });
  subsModal.addEventListener('hide.bs.modal', function() {
    alert(2);
  }, {
    once: true
  });

  myModal.show();
}
<!DOCTYPE html>
<html lang="en">

<head>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-Rx+T1VzGupg4BHQYs2gCW9It+akI2MM/mndMCy36UVfodzcJcF0GGLxZIzObiEfa" crossorigin="anonymous"></script>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
</head>

<body>
  <p><a href="#" onclick="showSubscriptionInfo();">Click HERE</a></p>
  <div id="myDiv"></div>
</body>
<!-- script src="js/bootstrap.bundle.min.js"></script -->
</html>

相关问题