/** * bulletin.js - M22公告栏主逻辑 * 修复URL编码问题 · 频道筛选正常 */ document.addEventListener('DOMContentLoaded', async () => { // 显示骨架屏 showSkeleton(); // 初始化频道 initializeChannels(); // 加载公告数据 await loadAnnouncements(); // 监听路由变化 window.addEventListener('hashchange', handleRouteChange); }); // 当前频道 let currentChannel = '全部'; // 所有公告数据 let allAnnouncements = []; // 初始化频道 function initializeChannels() { const channelTabs = document.querySelectorAll('.channel-tab'); // 从URL Hash恢复当前频道(需要解码) const rawHash = window.location.hash.slice(1) || '全部'; currentChannel = decodeURIComponent(rawHash); // 设置激活状态并绑定事件 channelTabs.forEach(tab => { const channel = tab.dataset.channel; if (channel === currentChannel) { tab.classList.add('active'); } else { tab.classList.remove('active'); } // 绑定点击事件 tab.onclick = function(e) { e.preventDefault(); const clickChannel = this.dataset.channel; console.log('点击频道:', clickChannel); // 直接设置中文hash,浏览器会自动编码 window.location.hash = clickChannel; }; }); } // 显示骨架屏 function showSkeleton() { const skeletonContainer = document.getElementById('skeleton-container'); if (skeletonContainer) { skeletonContainer.style.display = 'block'; } const bulletinContainer = document.querySelector('.bulletin-container'); if (bulletinContainer) { bulletinContainer.innerHTML = ''; } } // 隐藏骨架屏 function hideSkeleton() { const skeletonContainer = document.getElementById('skeleton-container'); if (skeletonContainer) { skeletonContainer.style.display = 'none'; } } // 渲染公告(带筛选功能) function renderAnnouncements(announcements) { // 隐藏骨架屏 hideSkeleton(); const container = document.querySelector('.bulletin-container'); if (!container) return; console.log('当前频道:', currentChannel, '总公告数:', announcements.length); // 根据当前频道筛选 let filtered = []; if (currentChannel === '全部') { filtered = announcements; } else { filtered = announcements.filter(a => a.channel === currentChannel); } console.log('筛选后条数:', filtered.length); // 如果没有公告,显示空状态 if (filtered.length === 0) { container.innerHTML = `