
本教程旨在帮助开发者理解如何通过 JavaScript 监听单选按钮的 change 事件,动态地改变 HTML 表格的显示与隐藏,从而实现表格样式的切换。通过修改 HTML 结构,将 class 属性改为 id 属性,并配合 JavaScript 代码,可以轻松实现这一功能。本文提供详细的代码示例和注意事项,帮助你快速掌握该技巧。
在 Web 开发中,经常需要根据用户的选择动态地改变页面的显示效果。其中,根据单选按钮的选择来切换表格样式是一种常见的需求。本文将详细介绍如何使用 JavaScript 监听单选按钮的 change 事件,并动态地控制表格的显示与隐藏,从而达到切换表格样式的目的。
首先,我们需要创建 HTML 结构,包含单选按钮和两个需要切换的表格。关键在于为表格添加 id 属性,以便 JavaScript 可以方便地操作它们。
<fieldset id="uberpruefung">
<legend style="font-weight: bold">Prüfung im Rahmen einer</legend>
<div>
<label for="stoerungbeh">Störungsbehebung</label>
<input type="radio" id="stoerungbeh" name="pruefung" value="stoerungsbehebung" onchange="changeStylePruefung(this)" checked><br>
</div>
<div>
<label for="hauptpruefung">Hauptprüfung</label>
<input type="radio" id="hauptpruefung" name="pruefung" value="hauptpruefung" onchange="changeStylePruefung(this)">
</div>
</fieldset>
<br><br>
<fieldset>
<legend style="font-weight: bold">In Ordnung</legend>
<div class='table-haupt' >
<table id='table-haupt' class='rg-table' summary='Hed'>
<tr>
<td>Haupt Table</td>
</tr>
</table>
</div>
<div class='table-stoerung'>
<table id='table-stoerung' class='rg-table-stoerung' summary='Hed'>
<tr>
<td>Stoerung Table</td>
</tr>
</table>
</div>
</fieldset>注意: 在上面的 HTML 代码中,我们为两个表格分别添加了 id 属性:table-haupt 和 table-stoerung。 同时,为了初始状态符合预期,请使用CSS控制表格的初始显示状态,例如:#table-haupt { display: none; } 和 #table-stoerung { display: block; }。
接下来,我们需要编写 JavaScript 代码来监听单选按钮的 change 事件,并根据选择的单选按钮的值来控制表格的显示与隐藏。
function changeStylePruefung(radiobutton) {
if (radiobutton.value === "stoerungsbehebung") {
document.getElementById("table-stoerung").style.display = "block";
document.getElementById("table-haupt").style.display = "none";
} else {
document.getElementById("table-stoerung").style.display = "none";
document.getElementById("table-haupt").style.display = "block";
}
}这段代码的核心在于:
将 HTML 和 JavaScript 代码整合在一起,就是一个完整的示例。 确保在 HTML 文件中引入 JavaScript 代码。
<!DOCTYPE html>
<html>
<head>
<title>表格样式切换</title>
<style>
#table-haupt { display: none; }
#table-stoerung { display: block; }
</style>
</head>
<body>
<fieldset id="uberpruefung">
<legend style="font-weight: bold">Prüfung im Rahmen einer</legend>
<div>
<label for="stoerungbeh">Störungsbehebung</label>
<input type="radio" id="stoerungbeh" name="pruefung" value="stoerungsbehebung" onchange="changeStylePruefung(this)" checked><br>
</div>
<div>
<label for="hauptpruefung">Hauptprüfung</label>
<input type="radio" id="hauptpruefung" name="pruefung" value="hauptpruefung" onchange="changeStylePruefung(this)">
</div>
</fieldset>
<br><br>
<fieldset>
<legend style="font-weight: bold">In Ordnung</legend>
<div class='table-haupt' >
<table id='table-haupt' class='rg-table' summary='Hed'>
<tr>
<td>Haupt Table</td>
</tr>
</table>
</div>
<div class='table-stoerung'>
<table id='table-stoerung' class='rg-table-stoerung' summary='Hed'>
<tr>
<td>Stoerung Table</td>
</tr>
</table>
</div>
</fieldset>
<script>
function changeStylePruefung(radiobutton) {
if (radiobutton.value === "stoerungsbehebung") {
document.getElementById("table-stoerung").style.display = "block";
document.getElementById("table-haupt").style.display = "none";
} else {
document.getElementById("table-stoerung").style.display = "none";
document.getElementById("table-haupt").style.display = "block";
}
}
</script>
</body>
</html>通过本教程,你学习了如何使用 JavaScript 监听单选按钮的 change 事件,并动态地控制表格的显示与隐藏,从而实现表格样式的切换。 关键在于使用正确的 HTML 结构,为表格添加 id 属性,并编写相应的 JavaScript 代码来操作这些元素。 掌握这些技巧,可以帮助你更好地构建动态、交互性强的 Web 页面。
以上就是基于单选按钮切换表格样式的实现教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号