https://technical.city/zh/cpu/best-price-to-performance?pg=1&sortfield=type&sortorder=up…
- User
https://technical.city/zh/cpu/best-price-to-performance?pg=1&sort_field=type&sort_order=up
我要做一个静态网页,跟这个一模一样,但是价格有我自己设计一个API来输入。
Assistant
要创建一个与给定链接类似的静态网页,同时允许通过自定义API输入价格,您可以按照以下步骤进行:
HTML结构
首先,创建基本的HTML结构:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"/> <meta content="width=device-width, initial-scale=1.0" name="viewport"/> <title>CPU性价比排行</title> <link href="styles.css" rel="stylesheet"/> </head> <body> <header> <h1>CPU性价比排行</h1> </header> <main> <table id="cpu-table"> <thead> <tr> <th>排名</th> <th>型号</th> <th>类型</th> <th>性能</th> <th>价格</th> <th>性价比</th> </tr> </thead> <tbody> <!-- 数据将通过JavaScript动态填充 --> </tbody> </table> </main> <script src="script.js"></script> </body> </html>CSS样式
创建一个
styles.css文件,添加以下样式:body { font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 20px; } header { background-color: #f4f4f4; padding: 1rem; text-align: center; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 12px; text-align: left; } th { background-color: #f2f2f2; font-weight: bold; } tr:nth-child(even) { background-color: #f9f9f9; }JavaScript逻辑
创建一个
script.js文件,实现数据获取和表格填充:// 模拟API调用获取CPU数据 async function fetchCPUData() { // 这里应该是您的实际API调用 // 为了演示,我们使用一个模拟的数据 return [ { rank: 1, model: "Intel Core i5-12400F", type: "桌面", performance: 24018, price: 1099, pricePerformance: 21.85 }, { rank: 2, model: "AMD Ryzen 5 5600X", type: "桌面", performance: 25065, price: 1399, pricePerformance: 17.92 }, // 添加更多CPU数据... ]; } // 填充表格 async function populateTable() { const tableBody = document.querySelector("#cpu-table tbody"); const cpuData = await fetchCPUData(); cpuData.forEach(cpu => { const row = tableBody.insertRow(); row.innerHTML = ` <td>${cpu.rank}</td> <td>${cpu.model}</td> <td>${cpu.type}</td> <td>${cpu.performance}</td> <td>${cpu.price}</td> <td>${cpu.pricePerformance.toFixed(2)}</td> `; }); } // 页面加载完成后执行 document.addEventListener("DOMContentLoaded", populateTable);实现自定义API
要实现自定义API来输入价格,您需要:
- 创建一个后端服务(如使用Node.js和Express)来处理API请求。
- 在后端实现一个接口,允许更新CPU价格。
- 修改前端JavaScript,添加一个函数来调用这个API更新价格。
例如,您可以在
script.js中添加:async function updatePrice(cpuModel, newPrice) { try { const response = await fetch('/api/update-price', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ model: cpuModel, price: newPrice }), }); if (response.ok) { console.log('价格更新成功'); // 重新加载数据并更新表格 await populateTable(); } else { console.error('价格更新失败'); } } catch (error) { console.error('API调用错误:', error); } }这个设计允许您创建一个与给定链接类似的静态网页,同时通过自定义API来更新价格。请注意,实际实现时需要考虑安全性、错误处理和用户界面等方面[1][2][5].
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.