选项卡案例

选项卡案例
<!DOCTYPE html><htmllang=“zh-CN”><head><metacharset=“UTF-8”/><metahttp-equiv=“X-UA-Compatible”content=“IE=edge”/><metaname=“viewport”content=“width=device-width, initial-scale=1.0”/><title>经典选项卡</title><style>/* 隐藏 */.hidden { display: none;}/* 显示 */.active { display: block;}.type >*.active,.content >*专业系统搭建点我wcqh.cn.active { backgroundcolor: lightgreen;}</style></head><body><divclass=“box”><divclass=“type”style=display: flex></div><divclass=“content”></div></div><scripttype=“module”>import* as tabs from ./modules/tabs.js;const type = document.querySelector(.type);const content = document.querySelector(.content); window.专业系统搭建点我wcqh.cnonload =()=> tabs.createTab(type, content); type.onclick = ev =>{ tabs.setContentStatus(ev, ev.target);};</script></body></html>

JS代码部分

// * 栏目数据const cates =[{ cid:1, cname:国际新闻},{ cid:2, cname:中国新闻},{ cid:3, cname:安徽新闻},];// * 列表数据const details =[{ key:1, cid:1, content:[{ title:俄罗斯宣布从赫尔松部分地区撤军, url:https://news.ifeng.com/专业系统搭建点我wcqh.cnc/8KoK54urn1k,},{ title:俄罗斯宣布从赫尔松部分地区撤军, url:https://news.ifeng.com/c/8KoK54urn1k,},{ title:俄罗斯宣布从赫尔松部分地区撤军, url:https://news.ifeng.com/c/8KoK54urn1k,},],},{ key:2, cid:2, content:[{ title:空战隐身无人僚机亮相!, url:https://news.ifeng.com/c/8KoeDFJXF1b,},{ title:空战隐身无人僚机亮相!, url:https://news.ifeng.com/c/8KoeDFJXF1b,},{t专业系统搭建点我wcqh.cnitle:空战隐身无人僚机亮相!, url:https://news.ifeng.com/c/8KoeDFJXF1b,},],},{ key:3, cid:3, content:[{ title:安康码”上新!家庭成员核酸情况查询更便捷, url:https://ah.ifeng.com/c/8KkGUDhAZNZ,},{ title:安康码”上新!家庭成员核酸情况查询更便捷, url:https://ah.ifeng.com/c/8KkGUDhAZNZ,},{ title:安康码”上新!家庭成员核酸情况查询更便捷, url:https://ah.ifeng.com/c/8KkGUDhAZNZ,},],},];f专业系统搭建点我wcqh.cnunction createTab(type, content){for(let i =0; i < cates.length; i++){const btn = document.createElement(button); btn.textContent = cates[i].cname; btn.dataset.key = cates[i].cid;if(i ===0) btn.classList.add(active); type.append(btn);}// 2. 生成内容for(let i =0; i < details.length; i++){const ul = document.createElement(ul); ul.data专业系统搭建点我wcqh.cnset.key = details[i].cid; ul.classList.add(i ===0?active:hidden);for(let k =0; k < details[i].content.length; k++){const li = document.createElement(li);const a = document.createElement(a); a.href = details[i].content[k].url; a.textContent = details[i].content[k].title; li.append(a); ul.append(li); content.append(ul);}}}fun专业系统搭建点我wcqh.cnction setBtnStatus(ev){const currentBtn = ev.target;[…ev.currentTarget.children].forEach(btn => btn.classList.remove(active)); currentBtn.classList.add(active);}function setContentStatus(ev, currentBtn){const lists = document.querySelectorAll(.content > ul); lists.forEach(list => list.classList.replace(active,hi专业系统搭建点我wcqh.cndden));const currentList =[…lists].find(list => list.dataset.key == currentBtn.dataset.key); currentList.classList.replace(hidden,active);}export{ createTab, setBtnStatus, setContentStatus };
批改老师:PHPz 批改状态:合格 老师批语:完成的不错
作者最新博文
2022-10-14 12:41:41

© 版权声明
THE END
喜欢就支持一下吧
点赞448 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容