以下是一个简单的带后台的域名售卖网页的示例代码:
前端代码(HTML、CSS和JavaScript):
<!DOCTYPE html>
<html>
<head>
<title>域名售卖网页</title>
<style>
</style>
</head>
<body>
<h1>域名售卖网页</h1>
<form id="domainForm">
<label for="domainName">域名:</label>
<input type="text" id="domainName" name="domainName" required>
<br>
<label for="price">价格:</label>
<input type="number" id="price" name="price" required>
<br>
<button type="submit">提交</button>
</form>
<div id="domainList">
</div>
<script>
const domainForm = document.getElementById('domainForm');
const domainList = document.getElementById('domainList');
domainForm.addEventListener('submit', function(event) {
event.preventDefault();
const domainName = document.getElementById('domainName').value;
const price = document.getElementById('price').value;
fetch('/api/domains', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ domainName, price })
})
.then(response => response.json())
.then(data => {
const domainItem = document.createElement('div');
domainItem.textContent = `${data.domainName} - ¥${data.price}`;
domainList.appendChild(domainItem);
domainForm.reset();
})
.catch(error => {
console.error('Error:', error);
});
});
fetch('/api/domains')
.then(response => response.json())
.then(data => {
data.forEach(domain => {
const domainItem = document.createElement('div');
domainItem.textContent = `${domain.domainName} - ¥${domain.price}`;
domainList.appendChild(domainItem);
});
})
.catch(error => {
console.error('Error:', error);
});
</script>
</body>
</html>
html
后端代码(使用Node.js和Express框架):
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
let domains = [];
app.get('/api/domains', (req, res) => {
res.json(domains);
});
app.post('/api/domains', (req, res) => {
const domain = req.body;
domains.push(domain);
res.json(domain);
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
javascript
请注意,这只是一个简单的示例,可能需要根据实际需求进行修改和完善。另外,还需要安装Node.js和Express框架来运行后端代码。