我正在根据OOP范式做一个项目,我不明白为什么它没有找到并添加一个类以便模式窗口变得可见,它在这段代码片段中给出了这样的错误
openPopup() {
this._selectedPopup.classList.add('popup_opened');
document.addEventListener('keydown', this._handleEscKey);
};
未捕获的类型错误:无法读取未定义的属性(读取“classList”)
class Popup {
constructor (selectorPopup) {
this._selectorPopup = document.querySelector(selectorPopup);
};
/* Открытие модального окна */
openPopup() {
this._selectedPopup.classList.add('popup_opened');
document.addEventListener('keydown', this._handleEscKey);
};
/* Закрытие модального окна */
closePopup() {
this._selectorPopup.classList.remove('popup_opened');
document.removeEventListener('keyup', this._handleEscKey);
};
/* Закрытие модального окна кнопкой 'Escape' */
_handleEscKey() {
if (evt.key = 'Escape') {
this.closePopup;
};
};
/* Постановка слушателей для закрытия модального окна по клику на "Overlay" и крестик */
setEventListeners() {
this._selectorPopup.addEventListener('click', () => {
if (evt.target.classList.contains('popup_opened')) {
this.closePopup;
};
if (evt.target.classList.contains('popup__close-icon')) {
this.closePopup;
};
});
};
};
class PopupWithForm extends Popup {
constructor (selectorPopup) {
super(selectorPopup);
//this._handleFormSubmit = handleFormSubmit;
};
/* Получение значений из полей ввода */
_getInputValues() {
this._inputList = this._selectorPopup.querySelectorAll('.popup__input');
this._newInputValues = {};
this._inputList.forEach((inputElement) => {
this._newInputValues = inputElement.value
});
console.log(this._newInputValues);
};
/* Сброс значений полей ввода */
resetForm() {
this._selectorPopup.reset();
};
/* Добавление сброса формы в родительский метод закрытия модального окна*/
closePopup() {
super.closePopup();
resetForm();
};
/* Установка слушателей */
setEventListeners() {
super.setEventListeners();
this._formSelector = this._selectorPopup.querySelector('.popup__input-container');
this._formSelector.addEventListener('submit', (event) => {
event.preventDefault()
});
//this._handleFormSubmit(this._getInputValues());
}
};
在我看来,问题不在于您指出的地方,而在于:
您
addEventListener没有将它作为参数传递给箭头函数evt,因此它不知道它是什么evt.target并考虑它undefined,因此发誓它undefined找不到classList