登录
首页 >  文章 >  前端

单选按钮切换表格样式教程

时间:2025-10-15 10:27:45 442浏览 收藏

对于一个文章开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《单选按钮切换表格样式教程详解》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

基于单选按钮切换表格样式的实现教程

本教程旨在帮助开发者理解如何通过 JavaScript 监听单选按钮的 change 事件,动态地改变 HTML 表格的显示与隐藏,从而实现表格样式的切换。通过修改 HTML 结构,将 class 属性改为 id 属性,并配合 JavaScript 代码,可以轻松实现这一功能。本文提供详细的代码示例和注意事项,帮助你快速掌握该技巧。

在 Web 开发中,经常需要根据用户的选择动态地改变页面的显示效果。其中,根据单选按钮的选择来切换表格样式是一种常见的需求。本文将详细介绍如何使用 JavaScript 监听单选按钮的 change 事件,并动态地控制表格的显示与隐藏,从而达到切换表格样式的目的。

HTML 结构

首先,我们需要创建 HTML 结构,包含单选按钮和两个需要切换的表格。关键在于为表格添加 id 属性,以便 JavaScript 可以方便地操作它们。

<fieldset id="uberpruefung">
    <legend style="font-weight: bold">Prüfung im Rahmen einer</legend>

    <div>
        <label for="stoerungbeh">Störungsbehebung</label>
        &lt;input type=&quot;radio&quot; id=&quot;stoerungbeh&quot; name=&quot;pruefung&quot; value=&quot;stoerungsbehebung&quot; onchange=&quot;changeStylePruefung(this)&quot; checked&gt;<br>
    </div>

    <div>
        <label for="hauptpruefung">Hauptprüfung</label>
        &lt;input type=&quot;radio&quot; id=&quot;hauptpruefung&quot; name=&quot;pruefung&quot; value=&quot;hauptpruefung&quot; onchange=&quot;changeStylePruefung(this)&quot;&gt;
    </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 代码

接下来,我们需要编写 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";
    }
}

这段代码的核心在于:

  1. 获取表格元素: 使用 document.getElementById() 方法根据 id 获取对应的表格元素。
  2. 控制显示与隐藏: 通过修改 style.display 属性来控制表格的显示与隐藏。display = "block" 表示显示,display = "none" 表示隐藏。

完整示例

将 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>
        &lt;input type=&quot;radio&quot; id=&quot;stoerungbeh&quot; name=&quot;pruefung&quot; value=&quot;stoerungsbehebung&quot; onchange=&quot;changeStylePruefung(this)&quot; checked&gt;<br>
    </div>

    <div>
        <label for="hauptpruefung">Hauptprüfung</label>
        &lt;input type=&quot;radio&quot; id=&quot;hauptpruefung&quot; name=&quot;pruefung&quot; value=&quot;hauptpruefung&quot; onchange=&quot;changeStylePruefung(this)&quot;&gt;
    </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>

注意事项

  • id 的唯一性: 确保 id 在 HTML 文档中是唯一的。
  • CSS 控制初始状态: 使用 CSS 控制表格的初始显示状态,避免页面加载时出现闪烁。
  • 代码可读性: 编写清晰、可读性强的代码,方便维护和调试。
  • 错误处理: 在实际项目中,需要添加适当的错误处理机制,例如,检查 document.getElementById() 是否返回 null。

总结

通过本教程,你学习了如何使用 JavaScript 监听单选按钮的 change 事件,并动态地控制表格的显示与隐藏,从而实现表格样式的切换。 关键在于使用正确的 HTML 结构,为表格添加 id 属性,并编写相应的 JavaScript 代码来操作这些元素。 掌握这些技巧,可以帮助你更好地构建动态、交互性强的 Web 页面。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《单选按钮切换表格样式教程》文章吧,也可关注golang学习网公众号了解相关技术文章。

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>