html - HTML:如何在每次单击 html 中的选项卡时刷新页面/文档?
问题描述
我准备了一个带有标签的 html 页面。每个选项卡都有输入字段。下面是html代码。
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial;}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
</style>
</head>
<body>
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'One')" id="defaultOpen">One</button>
<button class="tablinks" onclick="openTab(event, 'Two')">Two</button>
<button class="tablinks" onclick="openTab(event, 'Three')">Three</button>
</div>
<div id="One" class="tabcontent">
<h3>One</h3>
One :
<input type="number" min="0.0" id="txtOne" autofocus="autofocus" />
</div>
<div id="Two" class="tabcontent">
<h3>Two</h3>
Two :
<input type="number" min="0.0" id="txtTwo" autofocus="autofocus" />
</div>
<div id="Three" class="tabcontent">
<h3>Three</h3>
Three :
<input type="number" min="0.0" id="txtThree" autofocus="autofocus" />
</div>
<script>
function openTab(evt, Name) {
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(Name).style.display = "block";
evt.currentTarget.className += " active";
document.getElementById("txtOne").focus();
document.getElementById("txtTwo").focus();
document.getElementById("txtThree").focus();
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
</body>
</html>
我第一次单击每个选项卡并在输入字段中输入了一些值。在第二次单击每个选项卡时,输入字段具有输入的值。现在我想在单击选项卡的同时清除输入字段或重新加载/刷新页面,并希望设置自动聚焦输入字段。指导我的情况。提前致谢。
解决方案
尝试重置值
function reset(Name) {
document.getElementById("txt"+Name).value = '';
}
function openTab(evt, Name) {
reset(Name);
// your code
}
推荐阅读
- opencart-3 - 致命错误:未捕获 /journal/storage/vendor/twig/twig/src/Loader/ArrayLoader.php | opencart3.6
- python - Python - 深度优先搜索非递归方法
- arm64 - llvm/ompiler-rt sanitizer 不支持 aarch64 ilp32:asan_interceptors_vfork.S 与 aarch64 ilp32 不兼容
- flutter - Flutter 中是否有用于 3D 饼图的库?
- php - 为什么 openssl_decrypt 在输出数据中添加转义斜线?
- python - Pytest:最后输出更少?
- python - 如何使用 python 生成随机的十六进制颜色代码?
- python - 使用 CSV 的 Python 饼图
- java - 如何在 Java 中定义静态对象列表?
- html - 难以在 Bootstrap 中将标题横幅图像居中