写一个chatgpt的聊天界面网页代码
时间: 2024-01-24 14:15:54 浏览: 185
基于django实现chatgpt网页聊天功能源码+项目说明(提供登录界面,提供聊天次数控制及充值).zip
以下是一个简单的ChatGPT聊天界面网页代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>ChatGPT Chat</title>
<style>
.chat-container {
width: 400px;
height: 500px;
border: 1px solid #ccc;
overflow-y: scroll;
padding: 10px;
}
.message {
margin-bottom: 10px;
}
.user-message {
color: blue;
}
.bot-message {
color: green;
}
.input-container {
margin-top: 10px;
}
input[type="text"] {
width: 300px;
padding: 5px;
}
button {
padding: 5px 10px;
}
</style>
</head>
<body>
<div class="chat-container">
<div class="message user-message">User: Hello!</div>
<div class="message bot-message">Bot: Hi! How can I assist you today?</div>
</div>
<div class="input-container">
<input type="text" id="user-input" placeholder="Type your message here...">
<button onclick="sendMessage()">Send</button>
</div>
<script>
function sendMessage() {
var userInput = document.getElementById("user-input").value;
// Display user message
var userMessageElement = document.createElement("div");
userMessageElement.classList.add("message", "user-message");
userMessageElement.textContent = "User: " + userInput;
document.querySelector(".chat-container").appendChild(userMessageElement);
// Send user message to server and get response
// Replace the following code with your backend integration code
var botResponse = "Bot: This is a sample response.";
// Display bot message
var botMessageElement = document.createElement("div");
botMessageElement.classList.add("message", "bot-message");
botMessageElement.textContent = botResponse;
document.querySelector(".chat-container").appendChild(botMessageElement);
// Clear user input field
document.getElementById("user-input").value = "";
}
</script>
</body>
</html>
```
这是一个基本的HTML和CSS代码,用于创建一个仿ChatGPT聊天界面。它包括一个聊天容器、用户输入框和发送按钮。当用户点击发送按钮时,会将用户输入的消息显示在聊天容器中,并模拟从服务器获取机器人的回复并显示在聊天容器中。
阅读全文