javascript - 如何在 HTML/JavaScript 中动态添加选项卡?
问题描述
当谈到这个 HTML 东西时,我是个新手,所以我不知道如何让它工作。我一直在关注这个标签的描述https://www.w3schools.com/howto/howto_js_tabs.asp
我会定期发送 GET 请求并接收字符串 ID,因此我想为收到的每个唯一 ID 创建一个选项卡,但我不确定如何执行此操作。
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">SOAP5</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">GDAL</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">IDK</button>
</div>
<div id="London" class="tabcontent" style="height:100%;">
<!--#include virtual='navigation.htm' -->
</div>
<div id="Paris" class="tabcontent" style="height:100%;">
<!--#include virtual='navigation.htm' -->
</div>
<div id="Tokyo" class="tabcontent" style="height:100%;">
<!--#include virtual='navigation.htm' -->
</div>
<script>
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
function RefreshData() {
var xmlhttp = new XMLHttpRequest();
// Periodic data request
var intervalID = setInterval(function GetData() {
xmlhttp.open('GET', 'formInfo.htm', true);
if ((xmlhttp.readyState = xmlhttp.OPENED)) {
xmlhttp.send();
}
}, 1000);
xmlhttp.onreadystatechange = function () {
if (this.readyState === this.DONE) {
UpdateTabs(xmlhttp);
}
};
}
function UpdateTabs(xmlhttp) {
//How to create new tabs here?
}
解决方案
推荐阅读
- mysql - 极光引擎不支持跨区域只读副本集群加密
- python-3.x - 将熊猫列转换为格式不一致的日期时间
- sql - PostgreSQL 查询将间隔分解为周、天、小时、分钟、秒
- sockets - 在 kprobe BPF 中获取套接字文件描述符
- vue.js - 在 Vue 中,如何使用 require() 来引用 srcset 中的多个图像
- c++ - 您如何将内存作为对象类型读取?C++
- ruby-on-rails - 无法创建 RubyOnRails 项目(由于错误重试 fetcher)
- cmake - 如何从 QML 项目中为 cmake 自动生成 .qrc 文件
- r - 用 r 中的预定义值替换最小值和最大值
- sql - PostgreSQL - 获取与数组完全匹配的行