const btnCloseModal=document.querySelector('.closemodal'); //获取所有按钮 const btnOpenModal=document.querySelectorAll('.toggle-button'); console.log(btnOpenModal); 9 //展开按钮 10 const openModal=function(){ 11 12 13 14 15 16 modal.classList. remove('hidden'); overlay.classList.remove('hidden'); //关闭按钮 const closeModal=function(){modal.classList.add('hidden');overlay.classList.add('hidden');17 18 19 20 21 22 23 24 25 27 28 29 30 //给按钮添加点击事件 for(let i =@;i < btn0penModal.length;i++){ btnOpenModal[i].addEventListener('click',openModal) //给按钮添加关闭事件 btnCloseModal.addEventListener('click',closeModal); overlay.addEventListener('click',closeModal); 26 document.addEventListener('keydown',function(e){ if(e.key=='Escape'&&!modal.classList.contains('hidden'))) closeModal():代码有问题 修改代码
时间: 2023-08-11 11:04:37 浏览: 146
好的,我来帮您修改代码。首先,第 25 行的代码缺少一个左括号,应该修改为 `if (e.key == 'Escape' && !modal.classList.contains('hidden')) { closeModal(); }`。另外,第 27 行的 `btn0penModal` 应该修改为 `btnOpenModal`,因为变量名拼写错误。最终修改后的代码如下:
```javascript
const btnCloseModal = document.querySelector('.closemodal');
const btnOpenModal = document.querySelectorAll('.toggle-button');
const modal = document.querySelector('.modal');
const overlay = document.querySelector('.overlay');
const openModal = function() {
modal.classList.remove('hidden');
overlay.classList.remove('hidden');
}
const closeModal = function() {
modal.classList.add('hidden');
overlay.classList.add('hidden');
}
for (let i = 0; i < btnOpenModal.length; i++) {
btnOpenModal[i].addEventListener('click', openModal);
}
btnCloseModal.addEventListener('click', closeModal);
overlay.addEventListener('click', closeModal);
document.addEventListener('keydown', function(e) {
if (e.key == 'Escape' && !modal.classList.contains('hidden')) {
closeModal();
}
});
```
希望对您有帮助!
阅读全文