如何结合使用classname和函数和onclick

img

图片打开是可以看的


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .default {
            color: black;
        }

        .style1 {
            color: red;
        }

        .style2 {
            color: aqua;
        }
    </style>
</head>

<body>
    默认
    <input type="radio" onclick="changestyle(1)">
    样式一
    <input type="radio" onclick="changestyle(2)">
    样式二
    <input type="radio" onclick="changestyle(3)">

    <p id="text">你好,旧时光</p>
</body>
<script>
    function changestyle(val) {
        let text = document.getElementById("text");
        if (val == 1) {
            text.className = "default";
        } else if (val == 2) {
            text.className = "style1";
        } else {
            text.className = "style2";
        }
    }
</script>

</html>