Uncaught TypeError: Cannot read properties of undefined (reading ‘classList’)

Bootstrap 5.x使用Modal报错:Uncaught TypeError: Cannot read properties of undefined (reading 'classList'),那是由于使用的了jQuery,需要改一下写法。

原写法:

import Modal from 'bootstrap/js/src/modal'
if ($('#modalThemeBuy')) {
  const modalThemeBuy = new Modal(document.getElementById('modalThemeBuy'));
  $('#themeBuy').on('click', function () {
    modalThemeBuy.show()
  })
}

改为:

import Modal from 'bootstrap/js/src/modal'
if ($('#modalThemeBuy')) {
  const modalThemeBuy = $('#modalThemeBuy');
  $('#themeBuy').on('click', function () {
    modalThemeBuy.show()
  })
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注