当前位置: 首页 > article >正文

html TAB切换按钮变色、自动生成table--使用函数优化结构

<!DOCTYPE html>  
<head>  
<meta charset="UTF-8">  
<title>Dynamic Tabs with Table Data</title>  
<style>  
   /* 简单的样式 */    
   .tab-content {    
    display: none;    
    border: 1px solid #ccc;    
    padding: 1px;    
    margin-top: 0px;    
  }    
  .tab-content.active {    
    display: block;    
  }    
  button {  
        margin-right: 0px; /* 添加一些间隔 */  
        background-color: transparent; /* 初始背景色透明 */  
        border: 1px solid #ccc; /* 明确边框样式和颜色 */
        cursor: pointer; /* 鼠标悬停时显示手指形状 */  
        padding: 5px 10px; /* 增加内边距以便点击 */  
  }  
  .active-tab {    
    background-color: rgb(77, 218, 223); /* 选中时背景色变为蓝色 */  
    color: white; /* 文本颜色变为白色,以便在蓝色背景上可见 */
    border-color: rgb(47, 178, 183); /* 更改边框颜色以匹配背景色 */   
  }    
  table {    
    width: 100%;    
    border-collapse: collapse;    
  }    
  th, td {    
    border: 1px solid #ddd;    
    padding: 8px;    
    text-align: left;    
  }    
  th {    
    background-color: #f2f2f2;    
  }  
</style>  
</head>  
<body>  
<div id="tabs">  
  <!-- 选项卡头部将在这里动态生成 -->  
</div>  
<div id="tab-contents">  
  <!-- 选项卡内容(表格)将在这里动态生成 -->  
</div>  
  
<script>  
// 假设这是从后端获取的数据  
const data = {  
  "datasets": [  
    {  
      "name": "Dataset 1",  
      "data": [  
        { "num1": 1234, "num2": 5678, "status1": "on", "status2": "active" },  
        // 更多数据...  
      ]  
    },  
    {  
      "name": "Dataset 2",  
      "data": [  
        { "num1": 3456, "num2": 7890, "status1": "off", "status2": "inactive" },  
        // 更多数据...  
      ]  
<!DOCTYPE html>  
<html lang="en">  
<head>  
<meta charset="UTF-8">  
<title>Dynamic Tabs with Table Data</title>  
<style>  
    /* 简单的样式 */    
    .tab-content {    
     display: none;    
     border: 1px solid #ccc;    
     padding: 1px;    
     margin-top: 0px;    
   }    
   .tab-content.active {    
     display: block;    
   }    
   button {  
         margin-right: 0px; /* 添加一些间隔 */  
         background-color: transparent; /* 初始背景色透明 */  
         border: 1px solid #ccc; /* 明确边框样式和颜色 */
         cursor: pointer; /* 鼠标悬停时显示手指形状 */  
         padding: 5px 10px; /* 增加内边距以便点击 */  
   }  
   .active-tab {    
     background-color: rgb(77, 218, 223); /* 选中时背景色变为蓝色 */  
     color: white; /* 文本颜色变为白色,以便在蓝色背景上可见 */
     border-color: rgb(47, 178, 183); /* 更改边框颜色以匹配背景色 */   
   }    
   table {    
     width: 100%;    
     border-collapse: collapse;    
   }    
   th, td {    
     border: 1px solid #ddd;    
     padding: 8px;    
     text-align: left;    
   }    
   th {    
     background-color: #f2f2f2;    
   }  
 </style> 
</head>  
<body>  
<div id="tabs"></div>  
<div id="tab-contents"></div>  
  
<script>  
// 假设这是从后端获取的数据  
const data = {  
  "datasets": [  
    {  
      "name": "Dataset 1",  
      "data": [  
        { "num1": 1234, "num2": 5678, "status1": "on", "status2": "active" },  
        // 更多数据...  
      ]  
    },  
    {  
      "name": "Dataset 2",  
      "data": [  
        { "num1": 3456, "num2": 7890, "status1": "off", "status2": "inactive" },  
        // 更多数据...  
      ]  
    }  
  ]  
}; 
  
function toggleTab(button, index) {  
    const allTabs = document.querySelectorAll('#tabs button');  
    allTabs.forEach(tab => tab.classList.remove('active-tab'));  
    button.classList.add('active-tab');  
    showTabContent(index);  
}  
  
function showTabContent(index) {  
    const tabContents = document.querySelectorAll('.tab-content');  
    tabContents.forEach(tabContent => tabContent.classList.remove('active'));  
    tabContents[index].classList.add('active');  
}  
  
function createTable(data) {  
    const table = document.createElement('table');  
    const thead = table.createTHead();  
    const headerRow = thead.insertRow();  
    ['Num1', 'Num2', 'Status1', 'Status2'].forEach(text => {  
        const th = document.createElement('th');  
        th.textContent = text;  
        headerRow.appendChild(th);  
    });  
    const tbody = table.createTBody();  
    data.forEach(item => {  
        const row = tbody.insertRow();  
        ['num1', 'num2', 'status1', 'status2'].forEach(key => {  
            const cell = row.insertCell();  
            cell.textContent = item[key];  
        });  
    });  
    return table;  
}  
  
const tabsDiv = document.getElementById('tabs');  
const tabContentsDiv = document.getElementById('tab-contents');  
  
data.datasets.forEach((dataset, index) => {  
    const tabButton = document.createElement('button');  
    tabButton.textContent = `Tab ${index + 1} (${dataset.name})`;  
    tabButton.onclick = () => toggleTab(tabButton, index);  
    tabsDiv.appendChild(tabButton);  
  
    const tabContent = document.createElement('div');  
    tabContent.className = 'tab-content';  
    const table = createTable(dataset.data);  
    tabContent.appendChild(table);  
    tabContentsDiv.appendChild(tabContent);  
});  
  
// 默认显示第一个选项卡的内容和按钮  
if (data.datasets.length > 0) {  
    const firstTabButton = tabsDiv.querySelector('button');  
    firstTabButton.classList.add('active-tab');  
    const firstTabContent = tabContentsDiv.querySelector('.tab-content');  
    firstTabContent.classList.add('active');  
}  
</script>  
</body>  
</html>

在这里插入图片描述


http://www.kler.cn/news/323827.html

相关文章:

  • opencv:实现图像的自动裁剪与优化
  • Python中的机器学习:从入门到实战
  • GO入门核心基础
  • 【linux 多进程并发】linux下使用常见命令,来解析进程家族体系脉络
  • Java: 数据类型与变量和运算符
  • 网络管理:网络故障排查指南
  • 目前相对稳定的下载上传的方法(WebClient )(异步与进度)
  • MacOS Sequoia安装geant4.10.07
  • 20_BERT微调训练
  • 探索Python网络世界的利器:Requests-HTML库
  • Python自学查漏9.28
  • Spark 中 任务集 TaskSet 详解
  • 探索私有化聊天软件:即时通讯与音视频技术的结合
  • RSpec简析及应用案例
  • leetcode刷题day32|动态规划Part01(509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯)
  • uni-app进行微信小程序开发,快速上手
  • STM32 F1移植FATFS文件系统 USMART组件测试相关函数功能
  • 二、初步编写drf API
  • 太速科技-389-基于KU5P的双路100G光纤网络加速计算卡
  • linux系统的常用命令
  • 【系统规划与管理师】【案例分析】【考点】【答案篇】第10章 团队建设与管理
  • docker相关命令
  • 基于单片机的精确电压表DA-AD转换
  • 【笔记】神领物流day1.1.13前后端部署【未完】
  • JVM、JRE、JDK关系。HotSpot。JVM规范
  • 【R语言】fs 工具功能速查
  • 【项目经验分享】深度学习点云算法毕业设计项目案例定制
  • 【JavaEE】——内存可见性问题
  • 支付宝远程收款api之小荷包跳转码
  • 画两个数的平方和的曲线