JavaScript聊天界面: 动态添加聊天消息并实现复制功能
This function takes in two parameters: 'messageText' and 'isUserMessage'. The 'messageText' parameter is a string that represents the content of the message. The 'isUserMessage' parameter is a boolean value that indicates whether the message is from the user or the AI.
The function creates a new div element with the class 'message-bubble' and adds the appropriate class ('user-bubble' or 'ai-bubble') based on the value of 'isUserMessage'. It also creates a copy button that allows the user to copy the message to their clipboard.
The function then creates a span element to hold the message content and appends it to the 'messageBubble' div. If the last message in the 'userMessageContainer' is of the same type (user or AI) as the current message, the function appends the message content to the previous message's span element. Otherwise, the function creates a new message container div and appends the 'messageBubble' div to it, then appends the message container to the 'userMessageContainer'.
Finally, the function scrolls the 'userMessageContainer' to the bottom to show the latest message.
原文地址: https://www.cveoy.top/t/topic/mKsl 著作权归作者所有。请勿转载和采集!