From 15ce68cd6ea8a5c53e110d324a2156a909982501 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Sun, 26 Apr 2026 00:36:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=89=80=E6=9C=89=20onclick=20=E5=86=85?= =?UTF-8?q?=E8=81=94=E6=94=B9=E4=B8=BA=E4=BA=8B=E4=BB=B6=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 对话列表:新建按钮、对话项、删除按钮使用事件委托 - 对话页面:返回、清空、发送按钮使用 addEventListener - 快捷语句按钮使用事件监听 - 消息操作按钮(复制、重新生成、删除)使用事件监听 - 修复动态生成 HTML 后 onclick 无法正确绑定的作用域问题 --- www/app.js | 95 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 24 deletions(-) diff --git a/www/app.js b/www/app.js index 42e311c..d674b4e 100644 --- a/www/app.js +++ b/www/app.js @@ -13,16 +13,19 @@ let conversations = []; // 对话列表 let currentConversation = null; // 当前对话 let isLoading = false; -// DOM 元素 -const appContainer = document.getElementById('app'); -const messagesContainer = document.getElementById('messagesContainer'); -const messagesDiv = document.getElementById('messages'); -const userInput = document.getElementById('userInput'); -const sendBtn = document.getElementById('sendBtn'); -const welcome = document.getElementById('welcome'); +// DOM 元素(初始为 null,在 openConversation 时重新获取) +let appContainer = null; +let messagesContainer = null; +let messagesDiv = null; +let userInput = null; +let sendBtn = null; +let welcome = null; // 初始化 document.addEventListener('DOMContentLoaded', () => { + // 初始化 appContainer + appContainer = document.getElementById('app'); + // 从本地存储加载对话列表 const saved = localStorage.getItem('conversations'); if (saved) { @@ -68,19 +71,19 @@ function showConversationList() {
- -
+
${conversations.length === 0 ? '
暂无对话记录
' : conversations.map(conv => ` -
+
${escapeHtml(conv.title)}
${conv.messages.length} 条消息 · ${formatTime(conv.updatedAt)}
-
@@ -92,6 +95,29 @@ function showConversationList() { `; appContainer.innerHTML = listHtml; + + // 绑定事件 + const newChatBtn = document.getElementById('newChatBtn'); + if (newChatBtn) { + newChatBtn.addEventListener('click', createNewConversation); + } + + const conversationList = document.getElementById('conversationList'); + if (conversationList) { + conversationList.addEventListener('click', (e) => { + const item = e.target.closest('.conversation-item'); + const deleteBtn = e.target.closest('.conv-delete-btn'); + + if (deleteBtn) { + e.stopPropagation(); + const id = deleteBtn.getAttribute('data-id'); + deleteConversation(id); + } else if (item) { + const id = item.getAttribute('data-id'); + openConversation(id); + } + }); + } } // 创建新对话 @@ -121,14 +147,14 @@ function openConversation(id) { const chatHtml = `
-

${escapeHtml(currentConversation.title)}

-
@@ -152,10 +178,8 @@ function openConversation(id) { id="userInput" placeholder="输入消息..." rows="1" - onkeydown="handleKeyDown(event)" - oninput="autoResize(this)" > -
@@ -171,9 +195,17 @@ function openConversation(id) { sendBtn = document.getElementById('sendBtn'); welcome = document.getElementById('welcome'); - // 渲染消息 - renderMessages(); - userInput.focus(); + // 绑定按钮事件 + const backBtn = document.getElementById('backBtn'); + if (backBtn) backBtn.addEventListener('click', showConversationList); + + const clearBtn = document.getElementById('clearBtn'); + if (clearBtn) clearBtn.addEventListener('click', clearCurrentChat); + + // 绑定输入事件 + userInput.addEventListener('keydown', handleKeyDown); + userInput.addEventListener('input', (e) => autoResize(e.target)); + sendBtn.addEventListener('click', sendMessage); // 绑定快捷按钮事件 document.querySelectorAll('.quick-btn').forEach(btn => { @@ -183,6 +215,10 @@ function openConversation(id) { sendMessage(); }); }); + + // 渲染消息 + renderMessages(); + userInput.focus(); } // 删除对话 @@ -424,17 +460,17 @@ function renderMessages() { const actions = isUser ? `
- - +
` : `
- - + -
`; @@ -450,6 +486,17 @@ function renderMessages() { `; }).join(''); + // 绑定消息操作按钮事件(事件委托) + messagesDiv.querySelectorAll('.copy-btn').forEach(btn => { + btn.addEventListener('click', () => copyMessage(parseInt(btn.dataset.index))); + }); + messagesDiv.querySelectorAll('.regenerate-btn').forEach(btn => { + btn.addEventListener('click', () => regenerate(parseInt(btn.dataset.index))); + }); + messagesDiv.querySelectorAll('.delete-btn').forEach(btn => { + btn.addEventListener('click', () => deleteMessage(parseInt(btn.dataset.index))); + }); + scrollToBottom(); }