每次单击更改输出值时调用函数

时间:2018-06-17 08:38:53

标签: javascript function

我有这个练习,我坚持下去 他们让我做一个功能,每次点击按钮都会改变一天 这就是我所做的,但每次点击都无法想办法调用这个功能 问题是不使用任何图书馆 这是我的剧本

<!DOCTYPE HTML>
<html>

<head>
    <title> change it</title>
</head>

<body>
    <a>today is</a>
    <a id='demo'></a>
    <script>
        var s = 0

        function add(s) {
            if (s < 6) {
                s + 1
            } else {
                return s = 0
            }
        }
        var days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday", "Sunday"
        ]
        document.getElementById("demo").innerHTML = days_of_week[s];
    </script>
    <br><input type="button" value="next day" onclick="add(s)">
    <script>
        console.log(days_of_week[s]);
    </script>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

下面几点没有变化。我添加了存储新的s值并将changind day name移动到函数中。

&#13;
&#13;
<!DOCTYPE HTML>
<html>

<head>
    <title> change it</title>
</head>

<body>
    <a>today is</a>
    <a id='demo'></a>
    <script>
        var s = 0

        function add(s) {
            if (s < 6) {
                return s + 1
            } else {
                return s = 0
            }
        }
        var days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday", "Sunday"
        ]
        function changeDay(s) {
            document.getElementById("demo").innerHTML = days_of_week[s];
        }
        changeDay(s);
    </script>
    <br><input type="button" value="next day" onclick="window.s = add(s); changeDay(s);">
    <script>
        console.log(days_of_week[s]);
    </script>
</body>

</html>
&#13;
&#13;
&#13;