// 小红书热点榜单 JavaScript 逻辑 // DOM 元素 const loadingEl = document.getElementById('loading'); const errorEl = document.getElementById('error'); const hotListEl = document.getElementById('hotList'); const updateTimeEl = document.getElementById('updateTime'); // 页面加载完成后初始化 document.addEventListener('DOMContentLoaded', function() { loadData(); }); // 加载数据函数 async function loadData() { try { showLoading(); // 从API接口获取数据 const response = await fetch('https://60s.api.shumengya.top/v2/rednote'); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); if (data.code === 200 && data.data) { renderHotList(data.data); updateTime(); showSuccess(); } else { throw new Error('数据格式错误'); } } catch (error) { console.error('加载数据失败:', error); showError(); } } // 显示加载状态 function showLoading() { loadingEl.style.display = 'block'; errorEl.style.display = 'none'; hotListEl.style.display = 'none'; } // 显示错误状态 function showError() { loadingEl.style.display = 'none'; errorEl.style.display = 'block'; hotListEl.style.display = 'none'; } // 显示成功状态 function showSuccess() { loadingEl.style.display = 'none'; errorEl.style.display = 'none'; hotListEl.style.display = 'block'; } // 渲染热点列表 function renderHotList(hotData) { hotListEl.innerHTML = ''; hotData.forEach((item, index) => { const hotItem = createHotItem(item, index); hotListEl.appendChild(hotItem); }); } // 创建热点项目元素 function createHotItem(item, index) { const itemEl = document.createElement('div'); itemEl.className = 'hot-item'; // 添加点击事件 itemEl.addEventListener('click', () => { if (item.link) { window.open(item.link, '_blank'); } }); // 构建HTML内容 itemEl.innerHTML = `
热度: ${item.score}