w w w . w d . c h a t
🗣 Wd提问
怎么修改网页数据并且刷新不变
🤖 Ai回答
要修改网页的数据并在刷新后仍然保留修改的内容,通常需要将数据存储在服务器端或客户端的持久化存储中。以下是几种常见的方法:

1、使用服务器端存储(推荐)
如果你有权限访问服务器端代码,可以通过以下步骤实现:

步骤:
1、数据库存储:将用户修改的数据存储到数据库中。
2、后端接口:创建一个后端 API 接口,用于接收前端发送的数据并将其存储到数据库。
3、前端请求:在前端使用 JavaScript 的 `fetch` 或 `axios` 等工具向后端发送请求,更新数据。
4、页面渲染:每次页面加载时,从数据库中读取数据并动态渲染到页面上。

示例:
假设你有一个简单的待办事项列表应用:

用户可以在页面上添加、删除或编辑待办事项。
数据存储在 MySQL 数据库中。

后端代码(Node.js + Express):
javascript
const express = require('express');
const bodyParser = require('bodyparser');
const app = express();
app.use(bodyParser.json());

let todos = [
{ id: 1, text: 'Learn JavaScript' },
{ id: 2, text: 'Learn Node.js' }
];

// 获取所有待办事项
app.get('/api/todos', (req, res) = {
res.json(todos);
});

// 添加新待办事项
app.post('/api/todos', (req, res) = {
const newTodo = req.body;
todos.push(newTodo);
res.status(201).json(newTodo);
});

// 删除待办事项
app.delete('/api/todos/:id', (req, res) = {
const id = parseInt(req.params.id);
todos = todos.filter(todo = todo.id !== id);
res.sendStatus(204);
});

// 更新待办事项
app.put('/api/todos/:id', (req, res) = {
const id = parseInt(req.params.id);
const updatedTodo = req.body;
todos = todos.map(todo = (todo.id === id ? updatedTodo : todo));
res.json(updatedTodo);
});

app.listen(3000, () = console.log('Server running on port 3000'));

前端代码:
html
<!DOCTYPE html
<html lang=en
<head
<meta charset=UTF8
<meta name=viewport content=width=devicewidth, initialscale=1.0
<titleTodo List</title
</head
<body
<h1Todo List</h1
<ul id=todolist</ul
<form id=todoform
<input type=text id=newtodo placeholder=Add a new todo
<button type=submitAdd</button
</form

<script
const todoList = document.getElementById('todolist');
const todoForm = document.getElementById('todoform');
const newTodoInput = document.getElementById('newtodo');

// Fetch todos from server and render them
async function fetchTodos() {
const response = await fetch('/api/todos');
const todos = await response.json();
renderTodos(todos);
}

function renderTodos(todos) {
todoList.innerHTML = '';
todos.forEach(todo = {
const li = document.createElement('li');
li.textContent = todo.text;
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', () = deleteTodo(todo.id));
li.appendChild(deleteButton);
todoList.appendChild(li);
});
}

async function addTodo() {
const todoText = newTodoInput.value;
if (!todoText) return;

const response = await fetch('/api/todos', {
method: 'POST',
headers: { 'ContentType': 'application/json' },
body: JSON.stringify({ text: todoText })
});

const newTodo = await response.json();
renderTodos([...todos, newTodo]);
newTodoInput.value = '';
}

async function deleteTodo(id) {
await fetch(`/api/todos/${id}`, { method: 'DELETE' });
fetchTodos();
}

todoForm.addEventListener('submit', event = {
event.preventDefault();
addTodo();
});

fetchTodos();
</script
</body
</html

2、使用本地存储(不推荐长期使用)
如果你不想设置服务器端,可以使用浏览器的本地存储(如 `localStorage` 或 `sessionStorage`)来保存数据。这样数据只会在用户的浏览器中存储,并且不会影响其他用户的体验。

示例:
html
<!DOCTYPE html
<html lang=en
<head
<meta charset=UTF8
<meta name=viewport content=width=devicewidth, initialscale=1.0
<titleTodo List with LocalStorage</title
</head
<body
<h1Todo List</h1
<ul id=todolist</ul
<form id=todoform
<input type=text id=newtodo placeholder=Add a new todo
<button type=submitAdd</button
</form

<script
const todoList = document.getElementById('todolist');
const todoForm = document.getElementById('todoform');
const newTodoInput = document.getElementById('newtodo');

// Load todos from localStorage
let todos = JSON.parse(localStorage.getItem('todos')) [];

function renderTodos() {
todoList.innerHTML = '';
todos.forEach(todo = {
const li = document.createElement('li');
li.textContent = todo.text;
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', () = deleteTodo(todo.id));
li.appendChild(deleteButton);
todoList.appendChild(li);
});
}

function addTodo() {
const todoText = newTodoInput.value;
if (!todoText) return;

const todo = { id: Date.now(), text: todoText };
todos.push(todo);
localStorage.setItem('todos', JSON.stringify(todos));
renderTodos();
newTodoInput.value = '';
}

function deleteTodo(id) {
todos = todos.filter(todo = todo.id !== id);
localStorage.setItem('todos', JSON.stringify(todos));
renderTodos();
}

todoForm.addEventListener('submit', event = {
event.preventDefault();
addTodo();
});

renderTodos();
</script
</body
</html

总结
如果你需要持久化数据并且希望多个用户能够共享数据,建议使用服务器端存储(如数据库)。
如果只是简单的个人任务管理或不需要与其他用户共享数据,可以使用本地存储(如 `localStorage`)。
0
📍
IP地址 133.17.197.169
🔍
搜索次数 229
提问时间 2025-05-20 23:49:01

📣 商家广告

.gs域名

.gs域名

vps

vps

广告招商

广告招商

🛒 域名购买

热门提问

🌐 域名评估

最新挖掘

🖌 热门作画

🤝 关于我们

🗨 加入群聊
💬选择任意群聊,与同好交流分享

🔗 友情链接

🧰

站长工具

📢

温馨提示

本站所有 ❓️ 问答 由Ai自动创作,内容仅供参考,若有误差请用"联系"里面信息通知我们人工修改或删除。

👉

技术支持

本站由 🟢 豌豆Ai 提供技术支持,使用的最新版: 《豌豆Ai站群搜索引擎系统 V.25.10.25》 搭建本站。

上一篇 25442 25443 25444 下一篇