页面上有一个标题和一个按钮,用js实现点击按钮后使标题字体颜色变成红色

页面上有一个标题和一个按钮,用js实现点击按钮后使标题字体颜色变成红色

问题描述:

怎么把事件click和button绑定啊


<!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>
</head>

<body>

    <h1 id="id1">My Heading 1</h1>
    <p> <button οnclick="test()">点击这里!</button></p>
    <!-- <script type="text/javascript">
        var temp = 'red';

        function test() {

            if (temp === 'red') {
                document.getElementById('id1').style.color = 'blue';
                temp = 'blue';
            } else (temp === 'blue') {
                document.getElementById('id1').style.color = 'red';
                temp = 'red';
            }
        }
    </script> -->
    <script>
        var i = 1;

        function test() {
            i++;
            if (i % 2 == 0) {
                document.getElementById('id1').style.color = 'blue';
            } else {
                document.getElementById('id1').style.color = 'red';
            }
        }
    </script>

</body>

</html>

你的 onclick 的字母 o 是非法字符,从新打一遍

点击这里!