You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:点击CHECK TASKS按钮首次无响应,二次点击才显示Modal

问题解决:点击按钮第一次无反应,第二次才弹出Modal

问题描述

点击内容为"CHECK TASKS"的按钮时,第一次点击没有任何反应,第二次点击该按钮后,Modal才会显示,需要实现点击第一次就弹出Modal的效果。

代码展示

HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,300;0,700;1,400&display=swap" rel="stylesheet">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="container__panels">
            <div class="tasks panel">
                <h1 class="panel__headline">TASKS</h1>
                <button class="panel__button">CHECK TASKS</button>
            </div>
            <div class="events panel">
                <h1 class="panel__headline">EVENTS</h1>
                <button class=" panel__button">CHECK EVENTS</button>
            </div>
            <div class="reminder panel">
                <h1 class="panel__headline">REMINDERS</h1>
                <button class="panel__button" >CHECK REMINDERS</button>
            </div>
        </div>
    </div>
    <div class="modal">
        <button class="modal__exit">X</button>
    </div>
    <script src="classes.js"></script>
    <script src="script.js"></script>
</body>
</html>

CSS代码

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html{
    font-size: 10px;
    font-family: 'Roboto', sans-serif;
}
body{
    position: relative;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.container{
    position: relative;
    width: 1200px;
}

.container__panels{
    display: flex;
    justify-content: space-between;
    align-items: center;
   
}

.panel{
    text-align: center;
    flex: 0 1 300px;
    background-color: #F1F1F1;
    height: 400px;
    border-radius: 16px;
    box-shadow: 0 0 10px 0 rgba(0,0,0,0.20);
    
    display: grid;
    grid-template: repeat(5, 1fr) / 1fr;
}

.panel__headline{
    margin: 1em 0;
    font-size: 2rem;
}

.panel__button{
    cursor: pointer;
    grid-row-start: 5;
    justify-self: center;
    width: 200px;
    height: 50px;
    border: 0;
    border-radius: 12px;
    background-color: #474554;
    color: #F1F1F1;
}

.panel__button:hover{
    color: #929297;
}

/* MODAL */

.modal{
    visibility: hidden;
    position: absolute;
    height: 600px;
    width: 50%;
    border: 1px solid black;
    width: 70%;
    background-color: #fff;
    border: 0;
    border-radius: 15px;
    box-shadow: 0 0 10px 1px rgba(0,0,0,0.1);
}

.modal__exit{
    cursor: pointer;
    position: absolute;
    right: 0;
    top: 0;
    padding: 1em 2em;
}

classes.js代码

"use strict"

class Modal {
    constructor(buttons, modal, closeBtn) {
        this.buttons = buttons;
        this.modal = modal;
        this.closeBtn = closeBtn;
    }

    showModal() {
        this.buttons.forEach(button => {
            button.addEventListener("click", () => {
                if (button.textContent === "CHECK TASKS") {
                    this.modal.style.visibility = "visible";
                }
            });
        });
    }

    closeModal() {
        if (this.closeBtn) {
            this.closeBtn.addEventListener("click", () => {
                this.modal.style.visibility = "hidden";
            });
        }
    }
}

script.js代码

"use strict";

const buttons = document.querySelectorAll(".panel__button");
const modal = document.querySelector(".modal");
const closeBtn = document.querySelector(".modal__exit");

const modalInstance = new Modal(buttons, modal, closeBtn);

buttons.forEach(button => {
    button.addEventListener("click", () => {
        modalInstance.showModal();
    });
});

if (closeBtn) {
    closeBtn.addEventListener("click", () => {
        modalInstance.closeModal();
    });
}

问题原因

当前script.js中,是给每个按钮绑定了点击事件,只有当按钮被点击时才会调用showModal()方法,而showModal()的作用是给按钮添加点击监听。所以第一次点击按钮时,实际只是完成了监听的绑定,并没有触发显示Modal的逻辑;第二次点击时,绑定好的监听已经生效,才会触发Modal显示。

解决方案

修改script.js,去掉按钮点击事件里调用showModal()的逻辑,直接在页面初始化时调用modalInstance.showModal()和modalInstance.closeModal(),这样页面加载完成就会给按钮和关闭按钮绑定好事件监听,第一次点击就能触发Modal显示。

修改后的script.js代码:

"use strict";

const buttons = document.querySelectorAll(".panel__button");
const modal = document.querySelector(".modal");
const closeBtn = document.querySelector(".modal__exit");

const modalInstance = new Modal(buttons, modal, closeBtn);

// 页面加载时直接绑定事件,无需等待按钮点击
modalInstance.showModal();
modalInstance.closeModal();

内容的提问来源于stack exchange,提问作者Delfon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:35:34