当前位置:首页 > JavaScript

js实现tab栏目

2026-02-03 01:55:38JavaScript

使用HTML和CSS构建基础结构

HTML部分需要创建tab的标题和内容区域,通常使用无序列表<ul><div>实现。CSS负责样式布局,如高亮当前选中的tab。

<div class="tab-container">
  <ul class="tab-header">
    <li class="active">Tab 1</li>
    <li>Tab 2</li>
    <li>Tab 3</li>
  </ul>
  <div class="tab-content">
    <div class="content-item active">Content 1</div>
    <div class="content-item">Content 2</div>
    <div class="content-item">Content 3</div>
  </div>
</div>
.tab-header {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
}
.tab-header li {
  padding: 10px 20px;
  cursor: pointer;
  border: 1px solid #ddd;
  background: #f1f1f1;
}
.tab-header li.active {
  background: #fff;
  border-bottom: 1px solid #fff;
}
.tab-content .content-item {
  display: none;
  padding: 20px;
  border: 1px solid #ddd;
  border-top: none;
}
.tab-content .content-item.active {
  display: block;
}

通过JavaScript添加交互逻辑

使用事件监听实现点击切换功能。核心逻辑是移除所有元素的active类,再为当前点击的元素添加该类。

document.addEventListener('DOMContentLoaded', function() {
  const tabs = document.querySelectorAll('.tab-header li');
  const contents = document.querySelectorAll('.content-item');

  tabs.forEach((tab, index) => {
    tab.addEventListener('click', () => {
      // 移除所有active类
      tabs.forEach(t => t.classList.remove('active'));
      contents.forEach(c => c.classList.remove('active'));

      // 添加当前active类
      tab.classList.add('active');
      contents[index].classList.add('active');
    });
  });
});

动态生成Tab内容

若需从数据动态生成tab,可通过遍历数组创建DOM元素。

const tabData = [
  { title: 'News', content: 'Latest updates...' },
  { title: 'Weather', content: 'Forecast details...' }
];

function renderTabs() {
  const header = document.querySelector('.tab-header');
  const content = document.querySelector('.tab-content');

  tabData.forEach((item, i) => {
    // 创建标题
    const li = document.createElement('li');
    li.textContent = item.title;
    if (i === 0) li.classList.add('active');
    header.appendChild(li);

    // 创建内容
    const div = document.createElement('div');
    div.textContent = item.content;
    div.classList.add('content-item');
    if (i === 0) div.classList.add('active');
    content.appendChild(div);
  });
}

支持URL哈希定位

通过监听hashchange事件实现URL哈希切换tab,增强用户体验。

window.addEventListener('hashchange', () => {
  const hash = window.location.hash.substr(1);
  const targetTab = document.querySelector(`[data-tab="${hash}"]`);
  if (targetTab) targetTab.click();
});

// 修改点击事件,同步哈希
tabs.forEach(tab => {
  tab.addEventListener('click', () => {
    window.location.hash = tab.dataset.tab;
  });
});

js实现tab栏目

标签: 栏目js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('imag…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = documen…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…

js实现百叶窗

js实现百叶窗

使用CSS和JavaScript实现百叶窗效果 通过CSS动画和JavaScript事件监听可以实现百叶窗效果。核心思路是将内容区域分割为多个条状元素,通过控制它们的展开/折叠状态来模拟百叶窗。…